Make WordPress Core

Opened 7 years ago

Last modified 7 years ago

#43391 new defect (bug)

WP_User_Query bug

Reported by: zkingdesign's profile zkingdesign Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.4
Component: Query Keywords:
Focuses: template Cc:

Description

I've got an issue when doing a query with WP_User_Query and trying to list from a specific role AND using 'orderby' => 'post_count'. The query simply fails to list the correct results.

Here are the query arguments:

<?php
$args = array(
        'fields' => array( 'ID', 'user_login', 'display_name' ),
        'role' => 'expert',
        'number'         => $numPosts,
        'offset' => $page > 1 ?  $numPosts : 0,
        'order' => 'DESC',
        'orderby' => 'post_count'
        );

If I remove the 'role' argument from the query it works, but with the 'role' included it doesn't.

Change History (5)

#1 @chetan200891
7 years ago

@zkingdesign

Welcome to trac! Have you added new 'expert' role?

#2 @zkingdesign
7 years ago

@chetan200891

Yes, the 'expert' role is registered, and it's working OK, it only list users who have that role, but 'orderby' => 'post_count' ain't working.

#3 @chetan200891
7 years ago

I have tested with below query. It works fine for me. I am getting correct results.

<?php
$args = array(
        'fields' => array( 'ID', 'user_login', 'display_name' ),
        'role' => 'administrator',
        'number' => '10',
        'order' => 'ASC',
        'orderby' => 'post_count'
);

Ref. https://codex.wordpress.org/Class_Reference/WP_User_Query

#4 @zkingdesign
7 years ago

This is the query I get, when I dump the $query variable

https://gist.github.com/zkingdesign/2cd5588cee6539427539f8baf34c8f38

Query:

<?php
        $args = array(
                'fields' => array( 'ID', 'user_login', 'display_name' ),
        'role' => 'expert',
                'number'         => $numPosts,
        'offset' => $page > 1 ?  $numPosts : 0,
        'order' => 'DESC',
        'orderby' => 'post_count'
        );

$query = new WP_User_Query( $args );

        var_dump($query);

#5 @soulseekah
7 years ago

What do you get when you do var_dump( new WP_Query( $args ) ); on your original query that doesn't work? You can remove the results from the dumped object if there's sensitive information.

Edit: Sorry, nvm. I see the link now.

Last edited 7 years ago by soulseekah (previous) (diff)
Note: See TracTickets for help on using tickets.