Woocommerce – Changing the order of the upsell products

Having some trouble with a clients site.

We have a product with four upsell products linked, and they display on the site. Only problem is, those products are listed on the page in a seemingly random order (IE, no correlation between ID’s, post dates, names etc).

What we want, is for those products to be shown, as inputted in the back end.

This is currently the code set up we currently have going on (as far as I’m aware, this is vanilla WC, with one addition I will point out.

$upsells = $product->get_upsells();

if ( sizeof( $upsells ) == 0 ) return;

$meta_query = WC()->query->get_meta_query();

$args = array(
    'post_type'           => 'product',
    'ignore_sticky_posts' => 1,
    'no_found_rows'       => 1,
    'posts_per_page'      => $posts_per_page,
    'orderby'             => 'menu_order',
    'order'               => 'asc',
    'post__in'            => $upsells,
    'post__not_in'        => array( $product->id ),
    'meta_query'          => $meta_query

$products = new WP_Query( $args );

$upsells is an array, with a key of [post__in], that is array of the upsell id’s in the correct order.

From what is shown above, I can’t understand why this isn’t working. The only change that’s been made is the inclusion of the ‘order’ => ‘asc’ in the args, at a recommendation online to try and fix the issue.

I know this is off topic, via the woocommerce tag, but I’m hoping the core issue is with the WP query (I can’t imagine it being anything WC specific). If not, I’m more than happy to relocate.


Read more here: Woocommerce – Changing the order of the upsell products

Leave a Reply

Your email address will not be published. Required fields are marked *