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.