Custom Query Filter

Both Posts and Portfolio Widgets come with a feature-packed query control that allows you to select specific posts to show in the widget. But sometimes you need more control over the query and for that, we added a Custom Query filter to expose the WP_Query object and allow you to customize the query in any way you want.

Setting Up a Custom Filter

In Posts or Portfolio widget, give your query an ID, make sure it is unique unless you want the filter to run on multiple posts or portfolio widgets.

In this example the query ID is set to my_custom_filter, so when Elementor render’s the widget it will create a custom filter based on the query ID:

  • Posts & Portfolio Widgetselementor/query/my_custom_filter

Using the Custom Filter

After you have set up the Custom Query Filter you can use it to modify the query in the same way WordPress native pre_get_posts hook lets you modify the Query. Using the Custom Query filter is just like any other WordPress native action hook:

<?php
// Posts or Portfolio Widget
add_action( 'elementor/query/my_custom_filter', function( $query ) {
	// Modify the posts query here
} );

Examples

Multiple Post Types in Posts Widget

// Showing multiple post types in Posts Widget
add_action( 'elementor/query/my_custom_filter', function( $query ) {
	// Here we set the query to fetch posts with
	// post type of 'custom-post-type1' and 'custom-post-type2'
	$query->set( 'post_type', [ 'custom-post-type1', 'custom-post-type2' ] );
} );

Filter Posts by Post Meta in Portfolio Widget

// Showing post with meta key filter in Portfolio Widget
add_action( 'elementor/query/my_custom_filter', function( $query ) {
	// Get current meta Query
	$meta_query = $query->get( 'meta_query' );
	// Append our meta query
	$meta_query[] = [
		'key' => 'project_type',
		'value' => [ 'design', 'development' ],
		'compare' => 'in',
	];
	$query->set( 'meta_query', $meta_query );
} );

Most Popular Post by Comment count in Posts Widget

// Showing posts ordered by comment count in Posts Widget
add_action( 'elementor/query/my_custom_filter', function( $query ) {
	// Here we set the query to fetch posts with
	// ordered by comments count
	$query->set( 'orderby', 'comment_count' );
} );

Show Posts of Multiple statuses in Posts Widget

// Showing posts ordered by comment count in Posts Widget
// NOTE: Using this snippet may result in displaying private data
// Please use with caution.
add_action( 'elementor/query/my_custom_filter', function( $query ) {
	// Here we set the query to fetch posts with 
	// post status 'future' and 'draft'.
	// Refer to WP_Query documentation in WP codex for values list.
	$query->set( 'post_status', [ 'future', 'draft'] );
} );

Final Notes

  • The action hook provides two parameters:
    • $query – The WP_Query object.
    • $widget – The Current widget object.
  • You may need to refresh the editor to see the effect of the filter.
  • Deprecated hooks:
    • elementor_pro/posts/query/{$custom_query_id}
    • elementor_pro/portfolio/query/{$custom_query_id}

Get The Latest Updates

We have a lot more where that came from! Join 880,651 subscribers who stay ahead of the pack.
By entering your email, you agree to our Terms of Service and Privacy Policy.