I have registered third party shortcode [autor_role] which holds string with post author's user role.
I would like to use the value of the shortcode in Query filter because I need to filter out all posts of users belonging to a certain user role.
I have tried to achieve this via View loop:
<wpv-loop>
[wpv-if evaluate=" '[autor_role]' != 'inzerentneaktivni' "]
It worked only partially - desired posts are not shown, but there are "gaps" in the final results displays. As you can see in the screenshot attached - final two li's are not rendered (correct) but the pagination is incorrect (two gaps before next result page continues). Therefore I think the solution might be to use Query filter. Is there any possibility to filter via the shortcode value?
Thanks for help in advance
The gaps and broken pagination would be because your query is returning all of the results, and you are selectively hiding some of those results using the conditional shortcode, yet the pagination doesn't know you are hiding some of the results.
The correct way to do this is to use a Query Filter to exclude the posts from the results in the first place.
However, the you can only filter by a property of what you are querying. The post author ID is a property of posts, but the role of the post author is not.
So it won't be possible to set this up through the UI, you will need to use the API to modify the query.
Here is an example of code that you could add (at Toolset > Settings > Custom Code), where you would need to edit the View ID, and double-check the role that you want to exclude the posts of:
/**
* Exclude posts by authors with a given role.
*/
function tssupp_filter_query($view_args, $view_settings, $view_id) {
if (in_array($view_id, array(54))) { // Edit View ID
// get all users with specific role
$unwanted_users = get_users(array('role__in' => array('inzerentneaktivni'))); // Edit role
$unwanted_user_ids = wp_list_pluck($unwanted_users, 'ID');
$view_args['author__not_in'] = $unwanted_user_ids;
}
return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);
Hello Nigel,
Thanky you VERY much, that works like a charm!
One last thing - I need this snippet to run in several views. As I am PHP lama, is it possible to specify the view ID's in one condition or should I create new snippets for each view I need?