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
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
Share Improve this question asked Nov 15, 2018 at 12:43 user2115227user2115227 1614 silver badges16 bronze badges3 Answers
Reset to default 2There's quite a few issues with this code:
$query
is not defined in your callback function.role
is not a valid argument for a query.- You're passing the
$ids
variable incorrectly. - To query for multiple authors you need to use
author__in
. - 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' );
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 );
}
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' );