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?
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/
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.
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.
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 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.)
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: