gpt4 book ai didi

php - 通过来自 MySql 数据库的 JSON 在 WP_User_Query 中放置数组

转载 作者:行者123 更新时间:2023-11-28 23:12:39 24 4
gpt4 key购买 nike

我相当卡在某事上。我正在尝试通过 JSON 在 WP_User_Queryinclude 参数中放置一个 id 数组。

我的代码如下:

public function get_following_ids( $user_id = 0 ) {

$following = array();

$existing = $wpdb->get_var( $wpdb->prepare( "SELECT following FROM {$this->table} WHERE user_id = %d", absint( $user_id ) ) );

$following = json_decode( $existing );

return ( $following );

}

WP_User_Query 代码

public function author_following_list( $user_following_ids ) {      

$args = array(
...
'include' => json_encode( $user_following_ids )
);

$user_query = new WP_User_Query( $args );

$follow_users = $user_query->get_results();


ob_start();

if ( ! empty( $follow_users ) ) {
foreach ( $user_query->results as $user ) {
...
}
} else {
echo 'no users';
}

$content = ob_get_contents();
ob_end_clean();

echo $content;
}

然后在我使用的另一个函数中:

$following_ids      = $this->get_following_ids( $user_to_follow );

$this->author_following_list( $following_ids );

当我在 author_following_list 函数中使用 var_dump( $user_following_ids ); 时,它说 array(2) { [0]=> string(1) "2"[1]=> 字符串(1) "3"但是我得到 'no users' 作为输出。

有人可以让我走上解决这个问题的正确轨道吗?

最佳答案

还没有测试过,但也许下面的帮助:不要做 json_encode,因为 include 参数需要一个数组,而 $user_following_ids 已经是一个数组:

$args = array(
...
'include' => $user_following_ids
);

在文档中 https://codex.wordpress.org/Class_Reference/WP_User_Query它说:

$user_query = new WP_User_Query( array( 'include' => array( 1, 2, 3 ) ) );

关于php - 通过来自 MySql 数据库的 JSON 在 WP_User_Query 中放置数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45255891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com