I'm experiencing a problem with the following piece code:

global $wp_query;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$filter_args['paged'] = $paged;
$filter_args['posts_per_page'] = 2;

$args = array_merge( $wp_query->query_vars, $filter_args );
query_posts( $args );

This code is located at archive-prodcut.php file, inside of my theme/woocommerce folder.

It works correctly in order to show first 2 products and also to show WooCommerce pagination on the bottom.

do_action( 'woocommerce_after_shop_loop' );

However, when I click on each of the pages I'm getting a "Not found" error. When I tried putting an echo on the top of the archive-prodcut.php file I realized that it doesn't load. The url looks like www.site.com/product-category/cat-name/page/2

Thanks in advance!

  • Research this site archives for wp_query and pagination, lots of Q&A's about this – brasofilo Mar 15 '13 at 18:30
  • Why do you need to query_posts anyway? That is well known to mess with pagination and the query is already set to show an archive of products on the archive-product.php template. If you just want to alter the number of posts per page for products then look into pre_get_posts. – helgatheviking Mar 15 '13 at 18:35
  • I need to do that since there are a couple of filters to be applied (not shown in the code above). @brasofilo, thanks for your suggestion, but I'm unable to find a topic that covers and resolves my problem. – ZK_ Mar 15 '13 at 18:36
  • Maybe if you show the filters you'd like to apply there is an alternative with pre_get_posts? – helgatheviking Mar 15 '13 at 22:05
  • Same issues with pre_get_posts :) – ZK_ Mar 15 '13 at 22:43

It has to do with the posts_per_page being 2 and the backend WordPress Settings being a greater value. If you set your WordPress Settings to 2 it will work. But you may not want all your pages to have two posts. In that case you can alter the query using the code in this answer, Pagination Throws 404 just for that archive page.

Here is the code to put in your theme's function.php file.

if( !is_admin() ){  
add_action( 'pre_get_posts',  'set_per_page'  );
function set_per_page( $query ) {
    global $wp_the_query;
    if($query->is_post_type_archive('product')&&($query === $wp_the_query)){
    $query->set( 'posts_per_page', 1);
  return $query;

This changes the posts_per_page before running your query. Then when you get your posts it has your query with the dynamic variable posts_per_page. So if that number is 3, you will get your 3 products per page and no 404 error.

  • It's actually a dynamic variable, I used 2 just for example purposes. It's a value taken from a form with all filters applied via $_GET. – ZK_ Mar 15 '13 at 22:45
  • Right, so you still have the problem that if they enter a number lower than what is set in WordPress -> Settings -> Reading -> Max Number of Posts then you are going to get the 404. Use the code from the answer and replace tutorial with product in is_post_type_archive. The posts_per_page, 1 in the answer changes the WordPress Reading Settings only if the query is on archive-product.php. – ellenmva Mar 16 '13 at 0:16
  • That would work only if I'll able to get the $_GET data at this point and use it. Thanks, I'll try that later today. – ZK_ Mar 16 '13 at 17:01
  • You don't need the variable for the code in the answer, just in your query on the archive-product.php page. The set_per_page function doesn't need a variable, leave that at 1 and put that function in your theme's functions.php file. I edited my answer above to explain it better. – ellenmva Mar 17 '13 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.