3

I have 19 items in the "xyz" category currently but I am only able to see 5 of them at a time and shows pagination I don't want pagination.

Is there a way to make it to where more than 5 items show up on your first page of your xyz category ?

i added following code to my function.php of template

add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 10;' ), 19 );

but still its displaying 5 products .

Vel
  • 9,027
  • 6
  • 34
  • 66
Nick
  • 35
  • 1
  • 1
  • 5

4 Answers4

8

as of php 7.2 create_function is depricated. following filter is available to change the amount of products in the archive pages (i would only suggest to use that one, if it is different from your wordpress setting under settings>reading)

/**
  * WC: change products per page
  * @return int
  */
function so22835795_loop_shop_per_page() {
    return -1; //return any number, -1 === show all
};
add_filter('loop_shop_per_page', 'so22835795_loop_shop_per_page', 10, 0);
honk31
  • 3,895
  • 3
  • 31
  • 30
3

The loop_shop_per_page filter did not work for me as the theme was overriding it. So I used the woocommerce_product_query filter as follows:

<?php
add_action( 'woocommerce_product_query', 'so22835795_woocommerce_products_per_page', 1, 50 );

function so22835795_woocommerce_products_per_page( $query ) {
    if ( $query->is_main_query() ) {
        $query->set( 'posts_per_page', '5' );
    }
}
A.Jesin
  • 423
  • 3
  • 18
  • 1
    i just noticed your last tow arguments in add_action are a bit odd.. the 1 is the priority, default is 10. second one, 50, will set the amount of arguments, that will get passed from the action to your filter.. default is 1. i just looked, there are a maximum of 2 arguments available in `woocommerce_product_query` right now: `$q, $this` (and you actively use 1, `$query`, so i would simply remove both arguments, so it sticks to the defaults and will work the same) – honk31 Mar 28 '19 at 15:26
  • @a-jesin - `do_action( 'woocommerce_product_query', $q, $instance ); ` – dj.cowan Jun 26 '19 at 00:10
  • and another one: depending on the theme your are using, you still could overwrite their settings (if the devs made a good job) and use `loop_shop_per_page` with the `priority` setting. the third argument is the priority of your function, the lower the number, the earlier the execution: `add_filter('loop_shop_per_page', 'so22835795_loop_shop_per_page', $priority, 0);` (in reference to [my](https://stackoverflow.com/a/52385177/600121) answer). so i guess higher numbers would overwrite previous settings..? – honk31 Jan 13 '20 at 19:17
  • I had an issue with most recent woocommerce update. I tried to set the loop_shop_per_page and it didn't work. This answer worked for me. Posts Per Page is the key. I added it via snippets so I didn't need to modify the template files – Dustin Davis Sep 20 '20 at 00:26
2

This can be changed in the Reading Settings section of the Settings tab on the dashboard.

The option Blog pages show at most controls how many products can be seen. Changing that to 19 will allow all 19 products to be shown.

EDIT

add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 19;' ), 20 );

ANOTHER EDIT:

In the woocommerce/includes/class-wc-query.php page there is the following on line ~365

$q->set( 'posts_per_page', $q->get( 'posts_per_page' ) ? $q->get( 'posts_per_page' ) : apply_filters( 'loop_shop_per_page', get_option( 'posts_per_page' ) ) );

Change it to:

$q->set( 'posts_per_page', $q->get( 'posts_per_page' ) ? $q->get( 'posts_per_page' ) : apply_filters( 'loop_shop_per_page', '19') );
Howli
  • 12,291
  • 19
  • 47
  • 72
  • I have edited my post. I think you had the `add_filter` a bit wrong. I changed the `10` to `19` as that is the value that changes how many are displayed. – Howli Apr 03 '14 at 13:36
  • I tried this too . But there's no change in the output . – Nick Apr 03 '14 at 14:13
  • Are you using a specific theme or a default wordpress one? – Howli Apr 03 '14 at 14:26
  • @Nick I have edited my answer again, try that method (you might need to take out the `add_filter` option on the `functions.php` – Howli Apr 03 '14 at 14:36
  • Howlin thank you soo much for your answer. But thing is I figured out that the theme which I am using provides me a panel from there I am able to display as many product I wish. Intitially i was not aware of this but after being guided by my theme provider i was able to debug the problem. Thank you soo much Howlin your answer is also correct for default templates. – Nick Apr 04 '14 at 05:33
2

add this code to our theme's function.php file

add_filter('loop_shop_per_page', create_function('$cols', 'return 19;'));

it is working for me. Thakns

Narendra
  • 464
  • 2
  • 5
  • create_function is depricated on php7.2 – Nuno Sarmento Nov 27 '18 at 10:51
  • you should be able to use Anonymous Function explaines here https://stackoverflow.com/questions/48161526/php-7-2-function-create-function-is-deprecated, https://wordpress.org/support/topic/fix-for-create_function-deprecated-in-php-7-2/ – Narendra Apr 05 '19 at 10:34