Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Changing query with dropdown and pagination WordPress

  • SOLVED

I have a dropdown selection box which I'm using to change the number of posts being displayed. It works fine, but when I use the pagination as well it doesn't work. Here is the code I'm using.

<?php //The Sort Filter

if ($_REQUEST['sort'] == '10perpage' ){
$order = "&posts_per_page=10";
}

elseif ($_REQUEST['sort'] == '5perpage' ){
$order = "&posts_per_page=5";
}

else {
$order = "&posts_per_page=1";
}
//End sort filter ?>

<form method="post" id="order">
<select name="sort" onchange='this.form.submit()'>
<option value="10perpage" <?php if(isset($_REQUEST['sort']) && $_REQUEST['sort'] == '10perpage' ){ ?> selected="selected" <?php } ?> >Show 10</option> <!-- default -->
<option value="5perpage" <?php if(isset($_REQUEST['sort']) && $_REQUEST['sort'] == '5perpage' ){ ?> selected="selected" <?php } ?> >Show 5</option>
</select>
</form>


<?php

//echo $order;

$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

$custom_query_args ='post_type=open-days&paged='.$paged;

// The Query
wp_reset_query(); query_posts( $custom_query_args . $order );

// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;

/***PAGINATION*****/

global $wp_query;
$big = 999999999;

if(isset($_REQUEST['sort'])){
$sort = $_REQUEST['sort'];

$links = paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%'.$sort,
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'add_args' => array('sort' => $sort ),
'prev_text' => __('&larr;'),
'next_text' => __('&rarr;'),
'type' => 'list'
));

}else{
$links = paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%'.$sort,
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => __('&larr;'),
'next_text' => __('&rarr;'),
'type' => 'list'
));

}

if (count($links) > 0) {
echo '<nav class="pagination">';
echo $links;

echo '</nav>';
} //end if links 0


wp_reset_query(); ?>


Can someone explain what I'm doing wrong?

Thanks

Answers (3)

2015-07-02

Andrea P answers:

I can't see where you are limiting the query to loop only the selected number of posts.. is that intentionally omitted?

anyway, first thing I can see is that you are passing the sort choice as a form-post, while in the pagination-links function you are appending it as a GET url-query-string variable.. that's why when clicking on a page, it doesn't retrieve the value of the number of posts, as it's passed as a url-string and the code expect a post-variable.

I think you should change the sort-form method to be get, and then substitute everywhere you have
$_REQUEST['sort']

with:
$_GET['sort']


at that point it could actually work. let me know if there are other issues and I'll have a better look

p.s. anyway, I would set the value of the form options to be just the number, so you can directly place that value into the query
(when you place it in the query args, remember to do cast it as an integer doing intval($sort);

when you need to append the query-string to the pagination links, you can quickly build up another var like:
$sort_string = '&posts_per_page='.$sort;

it'd be surely easier than having to estrapolate the page number to use it in the query args, from the string which now you are passing from the form..


lukeseall comments:

Changing it to Get worked! Thanks. For the record, incase anyone else finds this helpful:-

<blockquote>I can't see where you are limiting the query to loop only the selected number of posts</blockquote>

See the $order variable in the following line

wp_reset_query(); query_posts( $custom_query_args . $order );

2015-07-02

Arnav Joy answers:

can you show me your site ?

2015-07-02

Jayaram Y answers:

Try this [[LINK href="http://wpquestions.com/question/showChronoLoggedIn/id/10796"]]Source[[/LINK]]


<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="5">5perpage</option>
<option value="10">10perpage</option>
</select>

</form>


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

$big = 999999999;

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']

)



));