Custom Query Filter

< All Topics

Custom Query option comes with Dynamic Listing widget and it is used to fetch the custom feed and list them with the same.

Table Of Contents

Examples

1) Multiple Post Types in Posts Widget

You can see multiple post types in the post widget.

function extra_function_name($query_args) {
$extra_query = array( ‘post_type’ => [‘post_type1′,’post_type2’]);
$query_args = array_merge($query_args,$extra_query);
return $query_args;
}
add_filter(‘your_fuction_id’, ‘extra_function_name’);

2) Filter Posts by Post Meta in Portfolio Widget

You can see posts with meta key filters in the portfolio widget.

function extra_function_name($query_args) {
if ( ! $meta_query ) {
$meta_query = array();
}
$extra_query = array(
‘relation’ => ‘AND’,
array(
‘key’ => ‘tp_portfolio’,
‘value’ => ‘my-value’,
‘compare’ => ‘=’
)
);
( ‘meta_query’, $extra_query );
return $query_args;
}
add_filter(‘your_fuction_id’, ‘extra_function_name’);

3) Most Popular Post by Comment count in Posts Widget

You can see posts ordered by comment count in the posts widget.

function extra_function_name($query_args) {
$extra_query = array( ‘orderby’ => ‘comment_count’,
‘order’ => ‘DESC’,
‘post_type’ => ‘post’,
‘post_status’ => ‘publish’,
‘posts_per_page’ => 5
);
$query_args = array_merge($query_args,$extra_query);
return $query_args;
}
add_filter(‘your_fuction_id’, ‘extra_function_name’);

4) Show Posts of Multiple statuses in Posts Widget

You can see posts ordered by comment count in the posts widget.
Note: While using this snippet may result in displaying private data.
You can use it with caution.

function extra_function_name($query_args) {
$extra_query = array( ‘post_type’ => [‘my-post-type’, ‘my-post-type2’],
‘order’ => ‘ASC’,
‘orderby’ => ‘title’,
‘post_status’ => [‘publish’, ‘pending’, ‘draft’, ‘auto-draft’, ‘future’, ‘private’, ‘inherit’, ‘trash’]
);
$query_args = new WP_Query($extra_query);
while ( $query_args->have_posts() ) : $query_args->the_post();
return $query_args;
}
add_filter(‘your_fuction_id’, ‘extra_function_name’);

5) Final Notes

The action hook provides two parameters:
$extra_query – The WP_Query object.
$query_args – The Post action object.

Do you want to rate this article?
0.5 out of 5 stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 100%
How can we improve this article?
Please submit the reason for your vote so that we can improve the article.
Need help?