• Hello,

    My client has requested a recent products category however they do not want to add the recent products to the category manually.

    As such I have created “recent” under product categories and I am just trying to modify the woocommerce loop to show the 12 most recent products on the taxonomy page /product-category/recent/

    My code is below however it is not working as expected.

    function recent_woo_query( $query ) {
        if ( is_product_category('recent') ) {
            $query->set( 'product_cat', '' );
            $query->set( 'orderby', 'date' );
            $query->set( 'order', 'desc' );
            $query->set( 'posts_per_page', 12 );
            $query->set( 'nopaging', 'true' );
            //$query->set( 'max_num_pages', 1 );
        }
    }
    add_action( 'woocommerce_product_query', 'recent_woo_query' );

    There are two issues:

    1. I cannot remove the pagination so we only see the 12 most recent products. If I have nopaging set to true then ALL products are shown rather than the just the most recent 12 and if I have nopaging set to false and max_num_pages set to 1 then that is also ignored and the pagination shows which means a large number of pages as there are a lot of products in the store. How can I remove the pagination other than hiding it with CSS?

    2. Using the code above the first page is showing 11 products, not 12 as per the subsequent pages. I can fix this on the first page by changing the number to 13 rather than 12 however then the subsequent pages which are showing up even thought I don’t want them too have 13 products. Why is the woocommerce loop showing 1 less product on the first page than the subsequent pages?

    So for now I have set the posts_per_page to 13, nopaging to false and hidden the pagination using CSS to effectly show only 12 posts, however I’m not comfortable with this messy workaround.

    Thanks very much for your assistance to make this work correctly.

    Nicole

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support con

    (@conschneider)

    Engineer

    Hi there,

    I would break it down to discern where exactly your problem lies. I take your control statement works and you are sure that the product category only works?

    If that is the case I would examine each $query->set( 'product_cat', '' ); – you might be passing the wrong parameters or you need a higher priority to win a race condition you are not aware of. I would also try and rule out any other modifications, for example by your custom theme(?).

    Kind regards,

    Thread Starter nicole2292

    (@nicole2292)

    Hi @conschneider

    Thanks so much for your response.

    The thing is that the query is working exactly as expected except for the number of posts retrieved / pagination. As in it returns all of the products, regardless of their category, ordered by date in the descending order. So that query is exactly as needed.

    It really is just the pagination parameters which are not working as expected. If I set $query->set( ‘posts_per_page’, -1 ); or $query->set( ‘nopaging’, ‘true’ ); Then I see all of the posts however I just want to limit to 12 posts.

    I have also tried adding $query->set( ‘no_found_rows’, true ); to stop the query after 12 however in this case nothing is returned at all.

    All I want is the first 12 from the exact same set of posts with no pagination.

    I have done some testing around product_cat as suggested and this does seem related to the 12 vs 13 issue as if I set it to one single product_cat or an array of product_cats then it does return only 12 on the first page however the pagination issue as described in point 1 above still remains even when I have used something like $query->set( ‘product_cat’, ‘example-category’ ); to restrict the query to only a single category.

    Lets not worry about the 12 vs 13 issue described in point 2 for now.

    How can I get the post limit and pagination to behave correctly so the query only returns a set number of posts and pagination is hidden?

    Thanks ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Paging parameters ignored when modifying the WooCommerce loop’ is closed to new replies.