Search code examples
phpwordpresswoocommerceproductcustom-taxonomy

Sort WooCommerce Products from a product category and custom meta_key


I'm successfully filtering ALL my WordPress posts by Likes (count) with a Custom Plugin (and meta_key) which also let me filter the most liked posts in categories. I display (query) the result in a custom page template. Everything works fine.

The Like function works also on WooCommerce Products. But so far I was not able to set up a page where I sort the Products (post_type) the same way in a specific shop category as I do it with my posts. The closest i came was to display the most liked posts on the page BUT the sorting for a category does not filter the posts - it displays the same as in the main page. The category List and the url call for the category links are working fine.

NOTE: THE URL query-string "product-cato" is a custom one - I'm using a ajax filter plugin which uses this query-string and the category id like .../?product-cato=6Please do not mix it up which product_cat for example

This is what I came up so far - beginning with the code for the posts (which works fine). Any idea how to solve this issue?

The query (works fine)

if (isset($_GET['category'])) {
    $args = array(
    'meta_key' => '_recoed',
    'meta_compare' => '>',
    'meta_value' => '0',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'category_name' => sanitize_text_field($_GET['category']),
    'paged' => $paged
    );
} 

 query_posts($args);

The Category List to Filter the Post in each Category (works fine)

<?php $categories = get_categories();

foreach($categories as $category) { ?>
    <li>
        <a class="popular-categories" href="<?php echo get_permalink(); ?>?category=<?php echo $category->category_nicename; ?>"><?php echo $category->name; ?></a> 
    </li>
<?php } ?>

Now the WooCommerce Query and Category Part where I am stuck

The Query for the Products post_type

    if (isset($_GET['product-cato'])) {
    $args = array(
    'meta_key' => '_recoed',
    'meta_compare' => '>',
    'meta_value' => '0',
    'post_type' => 'product',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'taxonomy' => sanitize_text_field($_GET['product_cat']),
    'paged' => $paged
);
        
query_posts($args);

The Category List to Filter the Post in each Shop-Category

<?php
    $product_categories = get_terms( 'product_cat' );
    $count = count($product_categories);

    foreach ( $product_categories as $product_category ) { ?>
        <li>
            <a class="popular-categories" href="<?php echo get_permalink(); ?>?product-cato=<?php echo $product_category->term_id; ?>"><?php echo $product_category->name; ?></a>
        </li>
    } 
?>

Solution

  • As Product Categories are a custom taxonomy 'product_cat', use a tax_query instead.

    So instead of wrong:

    'taxonomy' => sanitize_text_field($_GET['product_cat'])
    

    … use this (defining correctly the 'field' argument):

    'tax_query' => array( // the product category query
        array( 
            'taxonomy' => 'product_cat',
            'field'    => 'term_id', // (also 'name' or 'slug')  <==   <==   <==   <==   <== 
            'terms'    => sanitize_text_field($_GET['product-cato']),
        ),
    ),
    

    Check also in sanitize_text_field($_GET['product_cat']) that 'product_cat' is the right slug, as you use also product-cato

    If sanitize_text_field($_GET['product_cat']) is not a product category "slug", you should need to change 'field' => 'term_id', with the correct field type ('name' or 'slug').

    So your code should be (defining correctly the 'field' argument):

    if (isset($_GET['product-cato'])) {
        query_posts( array(
            'meta_key' => '_recoed',
            'meta_compare' => '>',
            'meta_value' => '0',
            'orderby' => 'meta_value_num',
            'order' => 'DESC',
            'paged' => $paged,
            'post_type' => 'product',
            //'posts_per_page' => 20,
            'post_status' => 'publish',
            // The product category query
            'tax_query' => array( 
                array( 
                    'taxonomy' => 'product_cat',
                    'field'    => 'term_id', // (also 'name' or 'slug') <==   <==   <==   <==
                    'terms'    => sanitize_text_field($_GET['product-cato']),
                ),
            ),
        ) );
    }
    

    It should work …