Skip Navigation

[Resolved] Query filter via registered third party shortcode value

This support ticket is created 6 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by tomasP 6 years ago.

Assisted by: Nigel.

Author
Posts
#1181437
Screenshot 2019-01-10 14.01.52.png

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' "]

  • [wpv-post-body view_template="loop-item-in-inzeraty"]
  • [/wpv-if]
    </wpv-loop>

    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

    #1181501

    Nigel
    Supporter

    Languages: English (English ) Spanish (Español )

    Timezone: Europe/London (GMT+00:00)

    Hi there

    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);
    

    (Note that you are using a very old syntax in your code sample; please review the documentation for the current syntax the next time you try to use conditional shortcodes: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/.)

    #1181589

    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?

    #1181593

    Nigel
    Supporter

    Languages: English (English ) Spanish (Español )

    Timezone: Europe/London (GMT+00:00)

    Just add the IDs of the Views to the existing array, e.g.

        if (in_array($view_id, array(54,99,200,512)))
    
    #1181626

    Thank you!!!!! Great support! Saved my day 🙂

    #1181627

    My issue is resolved now. Thank you!