Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #809624

    Hi great theme,

    I am using the Product slider on a page and i have selected multiple categories with the sort by most recent option selected.

    However, I would like for this shortcode to show only 1 (the most recent) item/product from each of the selected categories. Could you please help with a function that would enable the product slider loop only the most recent item from the selected categories.

    Awaiting your response.

    • This topic was modified 7 years, 5 months ago by adotopanuga.
    #810813

    Hey adotopanuga,

    Thank you for using Enfold.

    Please try this filter in the functions.php file:

    // get the first product in each category
    add_filter('avia_product_slide_query', 'avia_product_slide_query_mod', 10, 2);
    function avia_product_slide_query_mod($query, $params) {
    	$products = array();
    	$ids = array();
    
    	if(!empty($params['categories']))
    	{
    		$terms = explode(',', $params['categories']);
    	}
    
    	if(!empty($terms))
    	{
    		foreach( $terms as $term )
    		{
    			$args = array(
    				'posts_per_page' => 1,
    				'post_type' => 'product',
    				'no_found_rows' => true,
    				'update_post_meta_cache' => false,
    				'update_post_term_cache' => false,
    				'tax_query' => array(
    					array(
    						'taxonomy' => $params['taxonomy'],
    						'field' => 'id',
    						'terms' => $term
    					)
    				)
    			);
    
    			$products[] = get_posts($args)[0];
    		}
    	}
    
    	$products = array_unique($products, SORT_REGULAR);
    
    	if(!empty($products))
    	{
    		foreach( $products as $product )
    		{
    			$ids[] = $product->ID;
    		}
    	}
    
    	$query['post__in'] = $ids;
    
    	return $query;
    }

    Best regards,
    Ismael

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.