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

Creating a Personal Brand

The Struggle of Why and How to Create Your Personal Brand and Drive Success. Author: Dan Craddock September 26, 2017 What is a Personal Brand? Branding isn’t just for companies anymore. In the current market, more and more people are cultivating personal brands. Even within their own professional fields. Financial advisors, Realtors, Coaches, especially bloggers are getting themselves a personal

Read More »

SEO Report Breakdown

Breaking Down our Local SEO Report SEO Explained Our SEO clients get the standard monthly reports that all the other guys offer. But one thing we like to do to set ourselves apart is give our clients their highlight cover. This is the first page you’ll see on our SEO reports and it gives you a play-by-play of success and

Read More »

Food Trucks

Food Trucks: The Delicious Designs We Love Branding on the Go With summer winding down and cooler weather upon us, we’re starting to reflect back on all of the great food trucks we visited. Whether parked outside of a bar, a music festival, or an office, fresh food that comes to you is an absolute game changer. But it’s certainly

Read More »