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 the Post Blocks widget. You can add a more complex query structure to the Post Block Adv widget.
Here, we will take an example case to show your featured posts using the Post Blocks Adv widget.
Assumptions
The following article assumes that
- You have created a custom field (checkbox) for the post 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 the "Query" 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 that 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');
- 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 the Post Blocks Adv widget. You can modify it and return it from that filter. Here, we have added a 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.