【问题标题】:Placing arrays via JSON from MySql Database inside WP_User_Query通过 JSON 从 WP_User_Query 中的 MySql 数据库放置数组
【发布时间】:2017-12-28 13:57:32
【问题描述】:

我相当卡在某事上。我正在尝试通过 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_查询码

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 );

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

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

【问题讨论】:

    标签: php mysql arrays json


    【解决方案1】:

    尚未对其进行测试,但可能会有所帮助: 不要做 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 ) ) );
    

    【讨论】:

      猜你喜欢
      • 2015-07-28
      • 1970-01-01
      • 2016-02-21
      • 2012-05-30
      • 2017-01-07
      • 2011-09-11
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多