Metro Nova Creative Fredericksburg Graphic Design logo

Professional logo design and small business websites

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

Wix vs. WordPress

So you need to build a website for your business… great! That’s an excellent move to make, especially right now. Currently, a lot of business owners are shifting their business presence online, making it easier for everyone to access their services/products. It just makes sense. But what’s the best way to get a website going? We’ve got two very popular

Read More »

Website FAQ Series- Wix or WordPress?

What is the Difference Between Website Building Sites Like Wix and WordPress? Check out our second video in our FAQ series where we discuss how using WordPress can give you serious advantages while building your company’s online identity.  Wix or WordPress Video Transcript Hey, guys. Dan, from Metro Nova. So I’ve got another FAQ for website design clients. So we do

Read More »

I Scream, You Scream, We All Scream, For Marketing!

Every business owner needs a marketing budget and it looks different for each individual business. If you have a hot dog stand, a guy dancing in a hot dog costume is GREAT marketing…for those in the immediate vicinity of the guy dancing in the hot dog costume. How do you reach a wider audience? How do you let people know

Read More »