In this answer, You will learn how to get the results per page not working after page reload in your WooCommerce store.
Let’s imagine you got a dropdown like the following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// The template tag that you place in your ... template function WPSE116198PerPageSelect() { $selected = <?php <select name="woo_per_page" id="woo_per_page" size="3"> <?php foreach ( array( 24, 36, 48 ) as $val ) { ?><option <?php value="<?php echo $val; ?> selected( get_query_var( 'woo_per_page' ), $val ); ?>> <?php echo $val; ?> </option><?php } ?> </select> ?> } |
Then we need a callback for pre_get_posts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
add_action( 'pre_get_posts', 'WPSE116198PerPageCb' ); function WPSE116198PerPageCb( $query ) { if ( isset( $_POST['woo_per_page'] ) AND in_array( absint( $_POST['woo_per_page'] ) ), array( 24, 36, 48 ) ) ) { $per_page = absint( esc_attr( $_POST['woo_per_page'] ) ); $query->set( 'posts_per_page', $per_page ); } return $query; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.