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:

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


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

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}