D
D
Dmitry2020-02-26 14:47:41
WordPress
Dmitry, 2020-02-26 14:47:41

woocommerce wordpress 'Sort by latest' filter not working, how to solve the problem?

Hello, please tell me what could be the problem. According to the standard, the template contains "Sort by latest", it works on the main page and on the store page, but on the pages of product categories "Sort by latest" does not work.

It works only if you first switch to any other filter, for example by price, and then immediately switch to "Sort by latest" then it will work.

The question is why "Sort by latest" does not immediately work when the default is already selected and works on the shop page and on the main page.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-02-26
@castleJQ

Found the solution myself.

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 20, 1 );
function custom_catalog_ordering_args( $args ) {
    $product_category = $product_category; // <== здесь укажите конкретную категорию или напишие как у меня, тогда применяться ко всем категориям товаров.

    // Only for defined product category archive page
    if( ! is_product_category($product_category) ) return $args;

    // Set default ordering to 'date ID', so "Newness"
    $args['orderby'] = 'date ID';

    if( $args['orderby'] == 'date ID' )
        $args['order'] = 'DESC'; // Set order by DESC

    return $args;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question