我想添加我的自定义准备查询,但Add_Filter没有运行

时间:2013-02-05 作者:David Rosendo

我想操纵WP_User_Query(). 我希望用户按照自定义元字段和升序进行排序。

我已经对代码进行了正确的测试users.php 但既然这不是个好地方,我就想跑add_filter( \'prepare_query\', \'my_prepare_query\');

主题中的调用(“include”不存在):

$count_args = array(
    \'include\' => $include,
    \'number\' => 999999,
    \'fields\' => \'ID\',
    \'orderby\' => \'include\',
    \'order\' => \'ASC\'
);
$user_count_query = new WP_User_Query( $count_args );
我只是简单地添加了另一个elseifprepare_query 第433行,共user.php:

elseif ( \'include\' == $qv[ \'orderby\' ] ) {

    $this->query_from .= " INNER JOIN wp_usermeta ON wp_usermeta.user_id = $wpdb->users.ID";
    $this->query_where .= " AND wp_usermeta.meta_key = \'my_userpoints\'";
    $orderby = "wp_usermeta.meta_value";
}
现在为什么add_filter 不覆盖user.php 作用是否无法覆盖核心功能?

1 个回复
SO网友:kaiser

这个WP_User_Query 允许meta_query 与其他搜索完全相同WP_*_Query 课程。示例如下:

global $wpdb;
$author_search = new WP_User_Query( array(
    \'role\'       => \'subscriber\', 
    \'fields\'     => \'all_with_meta\',
    // if it\'s a digit/int/float, use \'meta_value_num\'
    \'orderby\'    => \'meta_value\',
    \'order\'      => \'ASC\',
    // you could try -1 as well.
    \'nuber\'      => 99999999,
    \'meta_query\' => array(
        \'key\'     => \'my_userpoints\',
        // only needed if you got a specific value you search for
        // \'value\'   => \'\',
        // only needed if you got a specific value you search for
        // and want to compare against it. For e.g. \'>\', \'<\', etc. See Codex.
        // \'compare\' => \'\',
        \'type\'    => \'CHAR\', // this is the default, use INT if you got an int/float value
    ),
) );
$author_list   = $author_search->get_results();
$author_count  = $wpdb->num_rows;

结束

相关推荐