Metro Nova Creative Fredericksburg Graphic Design logo
Specializing in gorgeous logos and professional-looking websites
Who You Gonna Call? (540) 369-3200
Get Posts with the Current Page’s Terms Using Elementor Custom Queries

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

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

More Design and Marketing Articles from the Metro Nova Creative Team

How to Write a Mission Statement

A mission statement is short description of a business’ purpose that guides the actions of the employees and attracts like-minded customers by explaining how the company intends to accomplish its purposes ethically.

Read More »

Writing a FAQ for your website

What is a FAQ? FAQ stands for Frequently Asked Questions. FAQS have been around since at least 1648, when Matthew Hopkins wrote wrote The Discovery of Witches as a list of questions and answers. Today the FAQ has migrated to web, and hyperlinking along with consistent formatting makes it easy for spiders like the GoogleBot to crawl and index for use in

Read More »