Pre_get_posts only show posts by administrator roles

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

Read more here:: Pre_get_posts only show posts by administrator roles

Leave a Reply

Your email address will not be published. Required fields are marked *