0

I customized the order in which the products are shown on the category pages (by SKU). My theme shows the previous/next product on the single product page, but it uses the default sorting (i think its the order in which the products have been added), so the previous/next products don't match the order of the actual products anymore.

This is my code. I tried adding 'orderby' => 'sku' in the wp_query array. It doesn't work.

 $the_query = new WP_Query( array( 'post_type' => 'product', 'p' => $id ) );

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        $valid_post = ( $valid == 'next' ) ? get_adjacent_post( 1, '', 0, 'product_cat' ) : get_adjacent_post( 1, '', 1, 'product_cat' );
        if ( empty( $valid_post ) ) return;
        $next_post_id = $valid_post->ID;
            $visibility = wc_get_product( $next_post_id );
        $stock = $visibility->is_in_stock();
        $visibility = $visibility->get_catalog_visibility();

    }
    // Restore original Post Data
    wp_reset_postdata();
}

I need help. Thanks in advance.

Adrian P.
  • 101
  • 3
  • 10
  • Maybe [this can be usefull](https://stackoverflow.com/questions/52273655/add-a-new-custom-default-ordering-catalog-option-in-woocommerce/52275842?r=SearchResults&s=2|42.6661#52275842) – LoicTheAztec Aug 13 '20 at 22:10
  • Thanks. I already have the custom order in place. I need to figure out how to get the adjacent posts using the SKU criteria. – Adrian P. Aug 14 '20 at 06:53

0 Answers0