1
votes

Im successfully filtering ALL my Wordpress posts by Likes (count) with a Custom Plugin (and meta_key) wich 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 cateogry 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 cateogy links are working fine.

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

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

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 stucks

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>
    } 
?>
1

1 Answers

2
votes

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 …