Listing Products from Two Categories

Essentially what i want should be pretty simple, all i need is a page to specifically display products from two categories rather than just the one.

This below displays one, as is standard:-

<?php echo do_shortcode('[mp_list_global_products per_page="16" category="womans" paginav="1"]');?>

But i have not seen or found a way to per say list "womans & shirts" together, it may not be achievable through shortcode and if there isn't, is there a way to list it via php

Hope someone can help

Kind Regards
~Matt

  • Hoang Ngo

    Hi @Matthew,

    I hope you are well today.

    For archive this feautre, we will need some modification on the source code.
    Please look at the file \plugins\marketpress\marketpress-includes\marketpress-ms.php, line 1248 you will see the function "mp_list_global_products"

    Now follow to the line 1273, you will see the code like this

    if ($category) {
        $category = $wpdb->escape( sanitize_title( $category ) );
        $query = "SELECT blog_id, p.post_id, post_permalink, post_title, post_content FROM {$wpdb->base_prefix}mp_products p INNER JOIN {$wpdb->base_prefix}mp_term_relationships r ON p.id = r.post_id INNER JOIN {$wpdb->base_prefix}mp_terms t ON r.term_id = t.term_id WHERE p.blog_public = 1 AND t.type = 'product_category' AND t.slug = '$category'";
      }

    Please update to

    if ($category) {
    $category = explode(',',$category);
    		for($i=0;$i<count($category);$i++){
    			$category[$i] = "'".$wpdb->escape( sanitize_title( $category[$i] ) )."'";
    		}
    		$category = implode(',',$category);
        $query = "SELECT blog_id, p.post_id, post_permalink, post_title, post_content FROM {$wpdb->base_prefix}mp_products p INNER JOIN {$wpdb->base_prefix}mp_term_relationships r ON p.id = r.post_id INNER JOIN {$wpdb->base_prefix}mp_terms t ON r.term_id = t.term_id WHERE p.blog_public = 1 AND t.type = 'product_category' AND t.slug IN ($category)";
    }

    So now, the shortcode should be like this
    echo do_shortcode('[mp_list_global_products per_page="16" category="woman,man" paginav="1"]');
    You can combine many categories by a comma(',').

    If you have any issues please don't hesitate to let us know so we can assist
    Best Regards
    Hoang

  • Matthew

    Hey @Hoang Ngo

    Yes products that only belong to both category's should display e.g.

    Product 1 - Category: Mens
    Product 2 - Category: Mens
    Product 3 - Category: Mens + Shorts

    So in turn if i call this:-

    echo do_shortcode('[mp_list_global_products per_page="16" category="mens, shorts" paginav="1"]');

    It would only display product 3 as it matches the criteria

    So it kind of filters it down but if i called the shortcode for just mens it would display all mens no matter what the extra category would be, hope this makes sense

    Thank you for your help, i appreciate it

    Kind Regards
    ~Matt

  • Hoang Ngo

    Hi @Matthew,

    Here is the new update

    $category = explode(',',$category);
    		for($i=0;$i<count($category);$i++){
    			$category[$i] = "'".$wpdb->escape( sanitize_title( $category[$i] ) )."'";
    		}
    		if(count($category) > 1){
    			$query = 'SELECT blog_id, p.post_id, post_permalink, post_title, post_content, p.post_date,count(post_id) as count FROM (';
    			foreach($category as $k=>$cat){
    				$query .= "SELECT blog_id, p.post_id, post_permalink, post_title, post_content,p.post_date FROM {$wpdb->base_prefix}mp_products p INNER JOIN {$wpdb->base_prefix}mp_term_relationships r ON p.id = r.post_id INNER JOIN {$wpdb->base_prefix}mp_terms t ON r.term_id = t.term_id WHERE p.blog_public = 1 AND t.type = 'product_category' AND t.slug = ($cat)";
    				if($k<(count($category)-1)){
    					$query.=' UNION ALL ';
    				}
    			}
    			$query.=') AS p GROUP BY p.post_id HAVING count >='.count($category);
    		}else{
    		$category = implode(',',$category);
        $query = "SELECT blog_id, p.post_id, post_permalink, post_title, post_content FROM {$wpdb->base_prefix}mp_products p INNER JOIN {$wpdb->base_prefix}mp_term_relationships r ON p.id = r.post_id INNER JOIN {$wpdb->base_prefix}mp_terms t ON r.term_id = t.term_id WHERE p.blog_public = 1 AND t.type = 'product_category' AND t.slug  = $category";
    		}

    It's a little bit complex know, but it should work :slight_smile:

    Best Regards
    Hoang

  • Matthew

    Hello @Hoang Ngo

    I just tried that and got this

    Parse error: syntax error, unexpected T_ELSE in /wp-content/plugins/marketpress/marketpress-includes/marketpress-ms.php on line 1309

    Maybe i've set it up wrong, here's the code for that area in total

    if ($category) {
    $category = explode(',',$category);
    	for($i=0;$i<count($category);$i++){
    		$category[$i] = "'".$wpdb->escape( sanitize_title( $category[$i] ) )."'";
    	}
    	if(count($category) > 1){
    		$query = 'SELECT blog_id, p.post_id, post_permalink, post_title, post_content, p.post_date,count(post_id) as count FROM (';
    		foreach($category as $k=>$cat){
    		$query .= "SELECT blog_id, p.post_id, post_permalink, post_title, post_content,p.post_date
    		FROM {$wpdb->base_prefix}mp_products p INNER JOIN {$wpdb->base_prefix}mp_term_relationships r
    			ON p.id = r.post_id INNER JOIN {$wpdb->base_prefix}mp_terms t
    			ON r.term_id = t.term_id
    		WHERE p.blog_public = 1
    			AND t.type = 'product_category'
    			AND t.slug = ($cat)";
    		if($k<(count($category)-1)){
    			$query.=' UNION ALL ';
    		}
    	}
    	$query.=') AS p GROUP BY p.post_id HAVING count >='.count($category);
    	}else{
    		$category = implode(',',$category);
    		$query = "SELECT blog_id, p.post_id, post_permalink, post_title, post_content
    		FROM {$wpdb->base_prefix}mp_products p INNER JOIN {$wpdb->base_prefix}mp_term_relationships r
    			ON p.id = r.post_id INNER JOIN {$wpdb->base_prefix}mp_terms t
    			ON r.term_id = t.term_id
    		WHERE p.blog_public = 1
    			AND t.type = 'product_category'
    			AND t.slug = $category";
    	}
    
     else if ($tag) {
        $query = $wpdb->prepare("
        	SELECT blog_id, p.post_id, post_permalink, post_title, post_content
        	FROM {$wpdb->base_prefix}mp_products p INNER JOIN {$wpdb->base_prefix}mp_term_relationships r
        		ON p.id = r.post_id INNER JOIN {$wpdb->base_prefix}mp_terms t
        		ON r.term_id = t.term_id
        	WHERE p.blog_public = 1
        		AND t.type = 'product_tag'
        		AND t.slug = %s", sanitize_title($tag)
        );
      } else {
        $query = "
        	SELECT blog_id, p.post_id, post_permalink, post_title, post_content
    			FROM {$wpdb->base_prefix}mp_products p
    			WHERE p.blog_public = 1";
      }

    Thank you for helping

    Kind Regards
    ~Matt

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.