Who You Gonna Call? (540) 369-3200

Get Posts with the Current Page’s Terms Using Elementor Custom Queries

I recently worked on a project for a client that wanted to have news-type posts about employees display on each employee’s page. Rather than go through the headache of making and maintaining an Elementor page for each individual employee, I created a single page template and used the following code to limit posts in the Elementor Posts Widget by term, parent page, and post type using Elementor Custom Queries and the built-in Query ID field.

It was a struggle to find some simple code that performed this very common task, so I’m posting here today to help others who are wondering how to limit query results to posts that share tags in Elementor.

How to Get Posts with the Current Page’s Terms Using Elementor Custom Queries

To call a custom query in Elementor, you need to write custom query and put it in functions.php of your theme or use Code Snippets as described below. Then you type the name of the custom query in the “Query ID” box of the Posts Widget as shown here:

Custom Query ID Field Elementor
Custom Query ID Field Elementor

Plugins I used:

Why did you use Code Snippets instead of editing functions.php?

On this particular website, the site was built by someone else on our team using Hello Elementor, without a child theme. To avoid any future problems with updates to Hello Elementor, I used Code Snippets instead. Plus, it’s free.

Why Tag Pages?

I decided to limit my dynamic results by term, and I needed the ability to tag pages with the same term as posts to link the posts back and forth, so I used Tag Pages. Plus it’s free, too.

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 limitpageparent 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:

More Design and Marketing Articles from the Metro Nova Creative Team

Friends of the Rappahannock - give local piedmont banner

Give Local Piedmont – Friends of the Rappahannock

As you may already know from a recent article, Metro Nova Creative LOVES our community. Each financial quarter, we choose an organization to donate a percentage of our sales to. This year, Q2 will be going to our friends at Friends of the Rappahannock. Not only do we want to give them money, we would love if YOU gave them

Read More »