Add your own filters to Post Blocks query

Do you love the Power of hooks and filters in WordPress?
We have added the ability to add your own filter and modify the query of Post Blocks widget. You can add a more complex query structure to the Post Block widget. 

Here we will take an example case to show your featured posts using Post Blocks widget. 

Following article assumes that

  • You have created a custom field (checkbox) for the post that is used to mark the post as featured. 
  • You know the basic functionality of the Post Blocks widget.
  • You know how WP_Query works in WordPress. Read more about WP_Query here.

Steps to add your own filter to the Post Block query. 

  • Under "Queries" section check for option "Query Filter"
  • Make sure you have read the description (warning message) below that field. 
  • Add your custom filter name in that field. In this case, we have placed "my_super_filter"
  • Now we will need to attach the filter to this function in functions.php of the active child theme.  
  • See the following code that we have added to fetch only those posts which are marked as featured. 
    		function my_super_filter_function($query_args){
                         $query_args['meta_key'] = 'is_featured';
                         $query_args['meta_value'] = 1;
                         return $query_args;
                   add_filter('my_super_filter', 'my_super_filter_function');
  • Now if you notice in the above code we have added a filter to "my_custom_filter". This function will receive "$query_args" as an argument that is passed in WP_Query by Post Blocks widget. You can modify it and return from that filter. Here we have added meta_key related filter to show only those posts that have meta_key "is_featured" set with "meta_value" = 1. 
  • It is recommended not to modify pagination and post count related parameters. They are already available in widget settings. 
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us