What I wanted to achieve is to list some products on a custom page which belongs to two common categories. This part is working fine, except the results per page dropdown. When I change the dropdown values to 24/36/48, after page reload the number of products shown is the same as on the initial pageload.
I have the following code:
$wp_query = new WP_Query(
array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'new-arrivals'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'earrings'
)
)
)
);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
woocommerce_get_template( 'archive-product.php' );
endwhile;
wp_reset_postdata();
wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number'));
I would like to get some directions on how to customise the loop to meet also the results per page functionality. I know I am missing sending and getting the dropdown values, but have no clue (yet) on how to do it with best practice, without cruel PHP and JS. There must be some WooCommerce styled best practice. If my code can be customised or if I’m on a totally wrong way. If so, where I made the mistake?
I thought this was will be the simple one, did not payed attention to the “posts per page” detail.
Let’s imagine you got a dropdown like the following
Then we need a callback for
pre_get_posts
:This code isn’t tested, I’m no WooCommerce expert and you’ll probably as well consider the
offset
, thenumposts
andpaged
/page
for pagination.Temporarily “cruel” PHP workout:
But I’m almost sure there must be a best practice solution.