Related Elementor Pro Query

0.0/5
<?php
add_action( 'elementor/query/related_products', function( $query ) {
		global $post;
		
		$terms = get_the_terms($post->ID, 'product_cat' );
		$product_cats = array();
	
		foreach ($terms as $term) {			
			if($term->parent !== 0){
			   	$product_cats[] = $term->slug;
			}
		}


		$query->set('post_type', 'product');
	  	$query->set('post__not_in', [$post->ID]); //Exclude current post from being selected
		
		$tax_query = array(
        'tax_query' => array(
            'relation' => 'AND',
		        array(
			        'taxonomy' => 'product_cat',
			        'field'    => 'slug',
					'terms'    => $product_cats,
					'operator'      => 'IN'
		        ),
	    	),
		);
				
		$query->tax_query->queries[] = $tax_query; 
   		$query->query_vars['tax_query'] = $query->tax_query->queries;
	

} );

Comments and reviews

{{ reviewsTotal }} Review
{{ reviewsTotal }} Reviews
{{ options.labels.newReviewButton }}
{{ userData.canReview.message }}

Description

This snippet is a custom query filter for elementor, that selects related products based on taxonomy.

Author

Tags