Backup of Elementor query code

Limitations

This code won’t work if you are trying to match multiple terms.

Elementor Custom Query: Limit results to pages with the same tag as the current page


This limitpage query forces the posts widget to display only items of type “page” that are published and share a tag with the current page or post. These items are listed alphabetically by title.

add_action( ‘elementor/query/limitpage’, function( $query ) {
$tag_objects = get_the_tags();
foreach( $tag_objects as $temp_tag) {
$tag_slugs[] = $temp_tag->slug;
}

$query->set( ‘tag_slug__in’, $tag_slugs );
$query->set( ‘post_type’, ‘page’ );
$query->set( ‘post_status’, ‘publish’);
$query->set( ‘order’, ‘ASC’);
$query->set( ‘orderby’, ‘title’ );
} );


Elementor Custom Query: Limit results to posts with the same tag as the current page

This limitpost query forces the posts widget to display only items of type “post” that are published and share a tag with the current page or post. These items are listed by newest first.

add_action( ‘elementor/query/limitpost’, function( $query ) {
$tag_objects = get_the_tags();
foreach( $tag_objects as $temp_tag) {
$tag_slugs[] = $temp_tag->slug;
}

$query->set( ‘tag_slug__in’, $tag_slugs );
$query->set( ‘post_type’, ‘post’ );
$query->set( ‘post_status’, ‘publish’);
$query->set( ‘order’, ‘DESC’);
$query->set( ‘orderby’, ‘date’ );
} );


Elementor Custom Query: Limit posts to pages with the same tag as the current page and with a specific post_parent

This limitpage parent query forces the posts widget to display only items of type “page” that are published, share a tag with the current page or post, and have a parent page/post with the post id of 11. These items are listed by title in alphabetical order.

add_action( ‘elementor/query/limitpageparent’, function( $query ) {
$tag_objects = get_the_tags();
foreach( $tag_objects as $temp_tag) {
$tag_slugs[] = $temp_tag->slug;
}

$query->set( ‘tag_slug__in’, $tag_slugs );
$query->set( ‘post_type’, ‘page’ );
$query->set( ‘post_status’, ‘publish’);
$query->set( ‘order’, ‘ASC’);
$query->set( ‘orderby’, ‘title’ );
$query->set( ‘post_parent’, 11 );
} );

More About Elementor Custom Queries

Here is more information and examples about how to use Elementor custom queries:

https://developers.elementor.com/custom-query-filter/
https://pupungbp.com/how-to-add-custom-query-to-post-widget-in-elementor/

Posted by Jenny Smith

I'm Jenny Smith. I blog about life on the 300+ acres of rolling farmland in Northern Virginia where I live. I like tomatoes, all things Star Trek, watercolor, and reading. I spend most days in the garden fighting deer and groundhogs while trying to find my life's meaning. I'm trying to be like Jesus -- emphasis on the trying.