0

I am trying to filter a query to only show posts that are posted by a user with the administrator role.

I've created a pre_get_posts action, and trying to use $query->set to filter the posts only in the category pages...

So far I have this

function remove_unvetted_authors() {
    $query->set( 'role', 'administrator' );
}

if (is_category()) {
    add_action( 'pre_get_posts', 'remove_unvetted_authors' );
}

but it's throwing a 500 error. I'm know 'role' isn't actually an arg for wp_query, but I can't find one that looks at the user role. :/

I also tried this:

function remove_unvetted_authors() {
    $ids = get_users( array('role' => 'administrator' ,'fields' => 'ID') );
    $query->set( 'author', '$ids' );
}

add_action( 'pre_get_posts', 'remove_unvetted_authors' );

But this also throws a 500 error

0

There's quite a few issues with this code:

  1. $query is not defined in your callback function.
  2. role is not a valid argument for a query.
  3. You're passing the $ids variable incorrectly.
  4. To query for multiple authors you need to use author__in.
  5. You're checking is_category() incorrectly.

For the first issue, the problem is that you're not getting $query from anywhere. You need to accept it as a parameter in the callback function:

function remove_unvetted_authors( $query ) {
    // $query->set( 'property', 'value' );
}

If you don't do that, $query is undefined, and trying to use ->set() on an undefined variable will result in a 500 error. This is the issue responsible for that error code. But, even if you fix that it still won't work until you resolve the other issues.

For the second issue, you can't query posts by author role like that. You will need to do what you're trying in your second example and query the author ids.

For the third issue, you're passing $ids incorrectly. It's a variable, so shouldn't have quotes around it:

$query->set( 'author', $ids );

That wouldn't throw a 500 error, but it still won't work.

The fourth issue is that you can't query multiple authors with the author argument. You need to use author__in:

$query->set( 'author__in', $ids );

Lastly, you're using is_category() incorrectly. The way you've structured it now, remove_unvetted_authors() would apply to all queries when you're viewing a category.

This would include menus and widgets. But, as written, even that wouldn't work correctly. This is because is_category() will not work outside a hooked function, because whether or not it is a category hasn't been determined yet when functions.php is run.

You only want to affect the main category queries, and you only want to do it on the front-end. So you should check is_admin() and also $query->is_category().

So after fixing all those issues, your code would look like this:

function remove_unvetted_authors( $query ) {
    if ( ! is_admin() && $query->is_category() ) {
        $user_ids = get_users( [
            'role'   => 'administrator',
            'fields' => 'ID'
        ] );

        $query->set( 'author__in', $user_ids );
    }
}
add_action( 'pre_get_posts', 'remove_unvetted_authors' );
0
function remove_unvetted_authors() {
    $ids = get_users( array('role' => 'administrator' ,'fields' => 'ID') );
    $query->set( 'author', '$ids' );
}

add_action( 'pre_get_posts', 'remove_unvetted_authors' );

This is going in the right direction but has a specific that is wrong. '$ids' - due to the single quotes, the variable does not get evaluated. Also get_users() returns an array. author expects an int.

Instead, with author__in (and no single quotes) it should work:

function remove_unvetted_authors() {
    $ids = get_users( array('role' => 'administrator' ,'fields' => 'ID') );
    $query->set( 'author__in', $ids );
}
-2

Please try to check below code:

function remove_unvetted_authors() {
// Query user data on admins
$administrators = new WP_User_Query( array( 'role' =>  'administrator' ) ); 

// Save admin IDs
foreach( $administrators->results as $u )
 $user_ids[] = $u->ID;

// Little cleanup
unset( $u, $administrators);

// Get all posts by these authors
$query = new WP_Query( array( 
  'post_type' => 'post',
  'author' => $user_ids 
) );
$posts = $query->posts;

//collect all post ids of that author by loop
$post_ids = array();
foreach($posts as $post) {
  $post_ids[] = $post->ID;    
}

$query->set('post__in', $post_ids);
}

add_action( 'pre_get_posts', 'remove_unvetted_authors' );
  • Why would you query post__in with user ids? – Jacob Peattie Nov 15 '18 at 13:05
  • user ids includes posts ids of administration and post__in include admin ids that have admin role and get those posts with pre_get_posts hook. – Prasad Wandre Nov 15 '18 at 13:07
  • What? Your code is going to get posts with the same IDs as administrator users. That's completely wrong. They need to query posts whose authors have those IDs. – Jacob Peattie Nov 15 '18 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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