Ask your WordPress questions! Pay money and get answers fast! (more info)

Post per page dropdown and pagination fix WordPress

  • SOLVED

Hi there,
I've got this problem. I have a page pased on template that shows custom posts in a loop.

I got this post per page dropdown working and pagination working but I need them to be connected somhow.

When I switch to 2 page from pagination dropdown sets to default and shows 6.

Drop down on 2 page displays blank page.

It shoud work like with products page. It should remember what how many product on page was set and work with pagination.

Anyone to help?

This is dropdown:

<form name="frm" class="db_posts_per_page_form" method="post" action="">
<select onchange="document.frm.submit()" name="db_posts_per_page" id="db_posts_per_page" style="width:50px;">
<option value="6">6</option>
<option value="9">9</option>
<option value="12">12</option>
<option value="15">15</option>
</select>
</form>


This query with dropdown variable.

if( isset($_REQUEST['db_posts_per_page'] ))

$posts_per_page = $_REQUEST['db_posts_per_page'];

else

$posts_per_page = 6; // default value
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array (
'post_type' => 'product',
'posts_per_page' => $posts_per_page,
'paged' => $paged,
);

$query = new WP_Query( $args );



This is pagination.

$total_pages = $query->max_num_pages;

if ($total_pages > 1){

$current_page = max(1, get_query_var('paged'));

$paginate_links = paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_next' => False,
'type' => 'list',
));
echo $paginate_links;

Answers (2)

2015-04-22

Andrea P answers:

you could try substituting the paginate links function with something like this:


paginate_links(array(
'base' => preg_replace('/\?.*/', '/', get_pagenum_link(1)) . '%_%',
'current' => $current_page,
'format' => 'page/%#%',
'total' => $total_pages,
'prev_next' => false,
'type' => 'list',
'add_args' => array(
'db_posts_per_page' => $_REQUEST['db_posts_per_page'],
'post_type' => 'product'
)
));


then you might also edit the per_page selector in order to pre-select the current item from the dropdown:


<select onchange="document.frm.submit()" name="db_posts_per_page" id="db_posts_per_page" style="width:50px;">
<option value="6" <?php if ($_REQUEST['db_posts_per_page']==6){ echo "selected"; } ?> >6</option>

<option value="9" <?php if ($_REQUEST['db_posts_per_page']==9){ echo "selected"; } ?> >9</option>

<option value="12" <?php if ($_REQUEST['db_posts_per_page']==12){ echo "selected"; } ?> >12</option>

<option value="15" <?php if ($_REQUEST['db_posts_per_page']==15){ echo "selected"; } ?> >15</option>

</select>



***** EDIT TO RESUME THE LATEST CORRECTIONS ******

The paginate_links function should be set like this


$big = 999999999; // need an unlikely integer

paginate_links(array(
'base' => preg_replace('/\?.*/', '', str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ) ) ,
'current' => $current_page,
'format' => 'page/%#%',
'total' => $total_pages,
'prev_next' => False,
'type' => 'list',
'add_args' => array(
'db_posts_per_page' => $_REQUEST['db_posts_per_page']
)

));


Andrea P comments:

Hi B-one,

any chance I can see the live website to better understand the issue that you are addressing?

thanks!


Andrea P comments:

Hi B-one,

I'm trying to guess what could be going wrong, but I'd need to look at the site and see the outputted urls of the pagination numbers, to actually understand why they are not built properly (in particular why they seem to be correctly built on first load, but apparently they go wrong when changing page from a page which has a page number in the url).

anyway, this is a blind guess that might work better (or might not..).


$big = 999999999; // need an unlikely integer
paginate_links(array(
'base' => preg_replace('/\?.*/', '/', str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ) ) ,
'current' => $current_page,
'format' => 'page/%#%',
'total' => $total_pages,
'prev_next' => False,
'type' => 'list',
'add_args' => array(
'db_posts_per_page' => $_REQUEST['db_posts_per_page']
)
));



if it doesn't work, please let me know the url of the archive so that I can see it in action.


b-one comments:

Hi Andrea,
it does work. Thank you for your time.

I've sent you PM message with link to the site. Did you get it?

Might have one more small thing to get done if you're interested.

2015-04-22

Arnav Joy answers:

try this

<?php @session_start();?>
<form name="frm" class="db_posts_per_page_form" method="post" action="">

<select onchange="document.frm.submit()" name="db_posts_per_page" id="db_posts_per_page" style="width:50px;">

<option value="6" <?php selected($_SESSION['posts_per_page'],6);?>>6</option>

<option value="9" <?php selected($_SESSION['posts_per_page'],9);?>>9</option>

<option value="12" <?php selected($_SESSION['posts_per_page'],12);?>>12</option>

<option value="15" <?php selected($_SESSION['posts_per_page'],15);?>>15</option>

</select>

</form>



and then



if( isset($_REQUEST['db_posts_per_page'] ))



$_SESSION['posts_per_page'] = $_REQUEST['db_posts_per_page'];



else



if(empty($_SESSION['posts_per_page'] ))$_SESSION['posts_per_page'] = 6; // default value

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array (

'post_type' => 'product',

'posts_per_page' => $_SESSION['posts_per_page'],

'paged' => $paged,

);



$query = new WP_Query( $args );


b-one comments:

@Andrea
Your solutions is close to perfect. Only thing is, when I change number of products from dropdown and go to 2 page, then back to 1 going again to 2 is not working. Link to page 2 is missing page argument, it looks like this /?db_posts_per_page=9
Maybe you could figure out why?


@Arnav
Thank you for your proposition, however it does not work. Drop down does switch number of products but after reload it always shows number 6 and does not pass value to page.