Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to manipulate the order of WP_User_Query(). I want the user to come out ordered by a custom meta field and by ascending order.

I have the code correctly working tested it on users.php but since this is not a good place to put it I wanted to run add_filter( 'prepare_query', 'my_prepare_query');

The call in the theme ('include' doesn't exist):

$count_args = array(
    'include' => $include,
    'number' => 999999,
    'fields' => 'ID',
    'orderby' => 'include',
    'order' => 'ASC'
);
$user_count_query = new WP_User_Query( $count_args );

I just simply added another elseif to prepare_query on line 433 of 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";
}

Now why is the add_filter not overwriting the user.php function? Is it not possible to overwrite core functions?

share|improve this question
    
Can you link to where prepare_query is in the core code? (I'm not sure it exists). –  Stephen Harris Feb 5 '13 at 13:45
    
its in wp-includes... Maybe I dont understand your question –  David Rosendo Feb 5 '13 at 13:47
    
@DavidRosendo ... where is wp-includes? The only prepare_query I see is a method of the WP_User_Search class, not a filter. –  s_ha_dum Feb 5 '13 at 15:47
    
I think you want the pre_user_query action to modify user query SQL directly. –  Milo Feb 5 '13 at 15:53
add comment

1 Answer

The WP_User_Query allows meta_query searches exactly like the other WP_*_Query classes. Example here:

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;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.