Hello
I have an archive and I want to set up to show only the posts of a given user role. But I don't see any option for that in the query filters. How can I achieve this?
Hi Jozsef,
Thank you for contacting us and I'll be happy to assist.
Your understanding is correct and this kind of filtering is not available, but you're welcome to submit this as a feature request:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/
Meanwhile, you can use "pre_get_posts" action ( ref: https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts ) and hook a custom function that first gets all the users from a particular role and then sets their IDs in the main query's "author__in" parameter ( ref: https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters ).
This way only the posts from those specific authors will be included in that archive.
Here is a forum reply with a good example of a code snippet that you can use to get started:
https://wordpress.stackexchange.com/a/319304
Note: You'll need to use WordPress conditional tags ( ref: https://codex.wordpress.org/Conditional_Tags ), to make the condition in that code to be more precise so that it only targets the archive's that you want this filtering on.
For more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
Hi Waqar
There was a solution for a problem that is somehow related to mine in this thread: https://toolset.com/forums/topic/conditional-display-based-on-user-role-of-post-author/
What do you think, could I use conditional somehow in the archive loop to show only the content that matches that one role?
Hi Jozsef,
The approach suggested in that thread can also work, but it comes with a few downsides.
The archive view will be returning a fixed number of posts (for example "10"), but on the actual pages, visitors will be seeing fewer posts than that, since only posts with certain criteria will be shown and others will be ignored.
If there is pagination in use, the actual number of posts per page will vary inconsistently, as a result.
Also the output number of shortcodes "wpv-items-count" and "wpv-found-count" would also not match the actual number of posts shown in the archives.
( ref: https://toolset.com/documentation/user-guides/views-shortcodes/ )
If the above points are not a concern, then yes you can use the approach of using the "wpv-conditional" shortcode, for filtering out posts from other roles.
( ref: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/ )
Yes, you are right, and this could cause problems.
What do you think about this approach? https://toolset.com/forums/topic/filter-view-by-user-role-and-post-type/
Hi Jozsef,
The approach suggested in this new thread ( https://toolset.com/forums/topic/filter-view-by-user-role-and-post-type/ ), is almost the same as what was shared in my earlier reply ( ref: https://toolset.com/forums/topic/query-filter-for-user-role/#post-1218533 ).
Since the view that you're using is not a post view but a WordPress archive view, "wpv_filter_query" can't be used and this is why I suggested using "pre_get_posts" instead.
Rest of the steps are pretty much the same.
I tried to rewrite the code you recommended:
function remove_unvetted_authors( $query ) {
if ( ! is_admin() && $query->is_archive() ) {
$user_ids = get_users( [
'role' => 'producator_neautorizat',
'fields' => 'ID'
] );
$query->set( 'author__in', $user_ids );
}
}
add_action( 'pre_get_posts', 'remove_unvetted_authors' );
Unfortunately it does exactly the opposite, it shows only those posts that are written bu authors of producator_neautorizat. I want to hide these posts from the archive. Please help me to understant what I am doing incorrectly...
I changed it a little bit further, now it hides the post of the user from that role:
function remove_unvetted_authors( $query ) {
if ( ! is_admin() && $query->is_archive() ) {
$user_ids = get_users( [
'role' => 'producator_neautorizat',
'fields' => 'ID'
] );
$query->set( 'author__not_in', $user_ids );
}
}
add_action( 'pre_get_posts', 'remove_unvetted_authors' );
I guess it is now correct, because as I understand it pre_get_post now shows authors that are not is that role.
But I have another problem now... Though I achieved not to show these posts in the archive now it disappears from the view that lists their own posts in the user's profile page (This user role is a temporary one, the admins first check the official documents they provide at registration and after that manually assign another user role: producator. That's why I need them to be able to create posts and show their own posts in the profile page but these will be public only when they get the producator user role.)
Hi Jozsef,
Thanks for the update and glad you were able to adjust the code to work as needed.
If your goal is to make this filtering stop only for a specific page, you can use "is_page", conditional tag:
https://developer.wordpress.org/reference/functions/is_page/
For example, suppose that you don't want that filtering function to work on a page with ID '5'. In that case, you can update the condition in that function from:
if ( ! is_admin() && $query->is_archive() ) {
To
if ( ! is_admin() && $query->is_archive() && !is_page( 5 ) ) {
Feel free to replace 5 with the actual ID of the profile page on your website.
Thank you, the problem is solved!