• Raviraya

    (@orbmix)


    I have created custom sorting option by stock date (which means when I change the status outstock to instock) it will give the product list by modified date. I got the list by using this code:

    $args[‘orderby’] = array(
    ‘meta_value_num’ => ‘DESC’,
    ‘title’ => ‘ASC’,
    );
    $args[‘meta_key’] = ‘_stock’;
    I would like to sort by stock update with menu_order, so I tried this:

    $args[‘orderby’] = array(
    ‘meta_value_num’ => ‘DESC’,
    ‘title’ => ‘ASC’,
    ‘menu_order’ => ‘ASC’,
    );
    $args[‘meta_key’] = ‘_stock’;
    But it doesn’t list properly.

    Can anyone help me, please? It would be appreciated.

    • This topic was modified 7 years ago by Raviraya.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Are you sure the product post type menu order values are non-zero? They usually default to zero. If non-zero, you are also specifying to order by title second. The menu order parameter would only have an effect for products that have both the same stock date and the same title. It seems to me this would be a rather rare occurrence.

    I’m not sure what values are stored as dates. If the date includes a time component, it would be very rare for stock dates to be exactly the same where title and menu order could come into play. I think the query is delivering what you asked for, even if it is not what you were expecting.

    If you’ve double checked the underlying data and are sure the ordering is wrong, it could be other code is modifying the query ordering via a filter or action hook. Check the query object’s request property and confirm the ORDER BY clause is as you specified.

    Thread Starter Raviraya

    (@orbmix)

    Hi bcworkz,

    Thanks for your reply.

    I am sure product post type menu order values are non zero, I have order numbers.
    actually the query is giving me by stock list by title. my question is is that possible to pass arguments in orderby meta_value_num and menu_order together? I tried its not giving any results.

    is there any other way to do? I am bit stuck in there :(. Thanks in advance.

    Moderator bcworkz

    (@bcworkz)

    Specifying multiple sort keys as an associative array is correct.

    So your query is not sorting by stock date either? Only by title? If the date is stored as a localized date string, depending on the format, ordering by meta_value_num may not have the desired effect. Also, if you also use the “meta_query” argument, it conflicts with “meta_key”.

    There’s a number of reasons why ordering may not work. If you could post the query’s request property (the resulting SQL), someone could likely spot the conflict.

    There are a couple drastic workarounds. One is to write your own SQL and use $wpdb methods. The other is to sort the return in PHP with usort(). This will be noticeably slower for any sizable list.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Woocommerce sort by meta_key stock with menu_order’ is closed to new replies.