Reputation: 203
I am trying to customize my WooCommerce product shop archive pages to first display all products that are marked favorite, and then all the rest of the products in the selected category.
I've already tried the following loop I placed in my archive-product.php file however I have 2 issues:
If I am on the /product-category/toys/
archive page, it shows products from all categories. It is only supposed to show products from the specific category (e.g. Toys).
Every paginated page first displays featured products, and then displays regular products. In reality, only the first page(s) are supposed to display featured products, and once all the featured products are displayed, it should then display the regular products. It seems like every page is showing two separate loops, as opposed to one loop.
ALSO, is this the preferred method of doing this, or should I be using pre_get_posts
?
<?php woocommerce_product_loop_start(); ?>
<?php
//CUSTOM LOOP
// Display featured Products first.
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1 ,
'tax_query' => array( array(
'taxonomy' => 'product_visibility',
'field' => 'term_id',
'terms' => 'featured',
'operator' => 'IN',
) )
) );
$featured_product_names = array();
$featured_product_id = array();
if ( $query->have_posts() ): while ( $query->have_posts() ): $query->the_post();
$product = wc_get_product( $query->post->ID );
wc_get_template_part( 'content', 'product' );
endwhile; wp_reset_query();endif;
// fetch other product which is not featured
$my_query = new WP_Query(array(
'post__not_in' => $featured_product_id,
'post_type' => 'product'
));
if ( $my_query->have_posts() ): while ( $my_query->have_posts() ): $my_query->the_post();
$product = wc_get_product( $query->post->ID );
wc_get_template_part( 'content', 'product' );
endwhile; wp_reset_query();endif;
?>
<?php woocommerce_product_loop_end(); ?>
I think I am on the right track but can't pin point the issue.
All assistance is appreciated!!
Upvotes: 1
Views: 380
Reputation: 1455
you have defined $featured_product_id
as array but not added postids in it to skip for other loop.Check below code and it will work.
<?php woocommerce_product_loop_start(); ?>
<?php
//CUSTOM LOOP
// Display featured Products first.
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1 ,
'tax_query' => array( array(
'taxonomy' => 'product_visibility',
'field' => 'term_id',
'terms' => 'featured',
'operator' => 'IN',
) )
) );
$featured_product_names = array();
$featured_product_id = array();
if ( $query->have_posts() ): while ( $query->have_posts() ): $query->the_post();
$featured_product_id[] = get_the_ID();
$product = wc_get_product( $query->post->ID );
wc_get_template_part( 'content', 'product' );
endwhile; wp_reset_query();endif;
// fetch other product which is not featured
$my_query = new WP_Query(array(
'post__not_in' => $featured_product_id,
'post_type' => 'product'
));
if ( $my_query->have_posts() ): while ( $my_query->have_posts() ): $my_query->the_post();
$product = wc_get_product( $query->post->ID );
wc_get_template_part( 'content', 'product' );
endwhile; wp_reset_query();endif;
?>
<?php woocommerce_product_loop_end(); ?>
Upvotes: 1