Support

Account

Home Forums ACF PRO Woocommerce Sorting Products by ACF Field

Helping

Woocommerce Sorting Products by ACF Field

  • Hello everyone,

    I am trying to sort the products in my product page by an ACF field. I want all the products with a value of this field displayed first and after that all other products.

    My fields name is “season” and if the value is “55” the post should be displayed in front of all other posts.

    I tried to change the query of “woocommerce_product_query” but I don’t know why it doesn’t work.

    Here is the code I tried:

    $query->set( 'meta_query', 
        array(
            'relation' => 'AND',
            'collection' => array(
                'key' => 'season',
                'value' => '55',
                'compare' => '='
            ),
            'normal' => array(
                'key' => 'season',
                'value' => '55',
                'compare' => '<'
            )
        ),
    );
    $query->set('orderby', array(
        'collection' => 'DESC',
        'normal' => 'DESC'
    ));

    I hope anyone can help me with my problem.
    Thanks in advance.

  • You are using two filters on the same field. The only thing that this is doing is to get all of the posts in descending value by the one field. Ordering posts is something that is done on the DB query and not something that WP does after getting the post. Basically your sql query ends up looking something like:
    ... ORDER BY season DESC, season DESC

    I can’t even begin to see a way to get what you want using this type of data.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.