Zac McDonald

Elementor Custom Related Posts Query

Use the Elementor posts widget to display related posts by tag from a specific custom post type such as product, course, event or quiz.

The Elementor default related posts query doesn’t have much flexibility. For instance you can create a widget that will show related posts by category or tag but this will bring up all posts that match those taxonomies. If you want a more specific related post widget you need to add a custom query. I will show you how to do this.

Create an Elementor custom query

I will create a related products by tag custom posts query in this example. This Elementor custom query will filter only the post type product with a tag matching the current post. You can see the query ID related_product_by_tag is set on line 1 and the post type product is set on line 9.

Change product on line 9 to your custom post type of choice to modify this custom search query function.

					add_action( 'elementor/query/related_product_by_tag', 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', 'product' );
	$query->set( 'post_status', 'publish');
	$query->set( 'order', 'DESC');
	$query->set( 'orderby', 'date' );

} );

Add the custom query function to your website

Add the custom query to your child theme functions.php file or as a snippet using a plugin.

Call your custom query from the Elementor posts widget

Add a Posts widget (located under PRO section)

Click Query and then input your custom query into the Query ID field.

Save your related posts widget as a global widget

This posts widget will now display products that share a tag with the current post. Save this as a global widget and you will have a related products widget ready whenever you need it. Right click on your posts widget, select Save as a Global, give it a name and click save.

Comments: Elementor Custom Related Posts Query

Leave a Reply

Your email address will not be published. Required fields are marked *

Elementor Custom Related Posts Query

WordPress Developer

Add an experienced WordPress Developer to your team.