Pay money and get answers for your WordPress questions (more info)

Combine Custom Sorting Filters

  • SOLVED

This is a continuation of a previous question I asked (http://wpquestions.com/question/show/id/7697)


Essentially now I need to combine my search filters. If someone clicks on "20" to show 20 posts per page, and then "Most Recent" to sort the posts by date, these 2 filters should be used.

This is the code I am currently using:

<div id="top-pagination">
<p>
<span class="left">Sort by:
<a href="<?php bloginfo('url'); ?>/celebrities/">Name<a> |
<a href="<?php bloginfo('url'); ?>/celebrities/?sort=date">Most Recent</a>
</span>

<span class="right">View:
<a href="<?php bloginfo('url'); ?>/celebrities/?show_posts=20">20</a> |
<a href="<?php bloginfo('url'); ?>/celebrities/?show_posts=50">50</a>
</span>
</p>
</div>

<div class="clear"></div>

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>

<?php
if ( !empty( $_REQUEST['show_posts'] ) ) {
$show_posts = $_REQUEST['show_posts'];
} else {
$show_posts = 10;
}

if ( !empty( $_REQUEST['sort'] ) ){
$sort = 'date';
$order = 'DESC';
}
else{
$sort = 'title';
$order = 'ASC';
}

$my_query_args=array(
'post_type'=>'speakers',
'posts_per_page' => $show_posts ,
'orderby'=> $sort ,
'order'=> $order,
'paged'=>$paged,
'taxonomy_name'=>'celebrity_types'
);

$wp_query = new WP_Query($my_query_args);

while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<article>
...Some article stuff in here
</article>

<?php endwhile; wp_reset_postdata();?>

<div id="pagination">
<p><span class="left"><?php echo 'Page '. $paged . ' of ' . $wp_query->max_num_pages; ?></span><?php prev_blog_page(); ?><?php next_blog_page(); ?></p>
</div>

Answers (1)

2013-01-18

Arnav Joy answers:

try this

<div id="top-pagination">

<p>

<span class="left">Sort by:

<a href="<?php bloginfo('url'); ?>/celebrities/">Name<a> |

<?php
if( !empty ($_REQUEST['show_posts']) )
$url = get_bloginfo('url').'/celebrities/?sort=date&show_posts='.$_REQUEST['show_posts'];
else
$url = get_bloginfo('url').'/celebrities/?sort=date';
?>

<a href="<?php echo $url;?>">Most Recent</a>

</span>



<span class="right">View:

<a href="<?php bloginfo('url'); ?>/celebrities/?show_posts=20">20</a> |

<a href="<?php bloginfo('url'); ?>/celebrities/?show_posts=50">50</a>

</span>

</p>

</div>



<div class="clear"></div>



<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>



<?php

if ( !empty( $_REQUEST['show_posts'] ) ) {

$show_posts = $_REQUEST['show_posts'];

} else {

$show_posts = 10;

}



if ( !empty( $_REQUEST['sort'] ) ){

$sort = 'date';

$order = 'DESC';

}

else{

$sort = 'title';

$order = 'ASC';

}



$my_query_args=array(

'post_type'=>'speakers',

'posts_per_page' => $show_posts ,

'orderby'=> $sort ,

'order'=> $order,

'paged'=>$paged,

'taxonomy_name'=>'celebrity_types'

);



$wp_query = new WP_Query($my_query_args);



while ($wp_query->have_posts()) : $wp_query->the_post(); ?>



<article>

...Some article stuff in here

</article>



<?php endwhile; wp_reset_postdata();?>



<div id="pagination">

<p><span class="left"><?php echo 'Page '. $paged . ' of ' . $wp_query->max_num_pages; ?></span><?php prev_blog_page(); ?><?php next_blog_page(); ?></p>

</div>


Anthony Moore comments:

Yes that works. What about doing it the other way, where if someone first clicks on "Most Recent" or "Name" and then clicks on "20" or "50"?


Arnav Joy comments:

I think that will work as usual , test it and let me know.


Anthony Moore comments:

Currently it does not work.


Arnav Joy comments:

please try this

<div id="top-pagination">



<p>



<span class="left">Sort by:

<?php

if( !empty ($_REQUEST['show_posts']) ) {

$url1 = get_bloginfo('url').'/celebrities/?show_posts='.$_REQUEST['show_posts'];
$url2 = get_bloginfo('url').'/celebrities/?sort=date&show_posts='.$_REQUEST['show_posts'];
}

else {

$url1 = get_bloginfo('url').'/celebrities/';
$url2 = get_bloginfo('url').'/celebrities/?sort=date';
}

?>




<a href="<?php echo $url1;?>">Name<a> |




<a href="<?php echo $url2;?>">Most Recent</a>



</span>







<span class="right">View:

<?php

if( !empty ($_REQUEST['sort']) ) {
$url1 = get_bloginfo('url').'/celebrities/?show_posts=20&sort='.$_REQUEST['sort'];
$url2 = get_bloginfo('url').'/celebrities/?show_posts=50&sort='.$_REQUEST['sort'];
}
else{
$url1 = get_bloginfo('url').'/celebrities/?show_posts=20';
$url2 = get_bloginfo('url').'/celebrities/?show_posts=50';
}

?>

<a href="<?php echo $url1;?>">20</a> |



<a href="<?php echo $url2;?>">50</a>



</span>



</p>



</div>







<div class="clear"></div>







<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>







<?php



if ( !empty( $_REQUEST['show_posts'] ) ) {



$show_posts = $_REQUEST['show_posts'];



} else {



$show_posts = 10;



}







if ( !empty( $_REQUEST['sort'] ) ){



$sort = 'date';



$order = 'DESC';



}



else{



$sort = 'title';



$order = 'ASC';



}







$my_query_args=array(



'post_type'=>'speakers',



'posts_per_page' => $show_posts ,



'orderby'=> $sort ,



'order'=> $order,



'paged'=>$paged,



'taxonomy_name'=>'celebrity_types'



);







$wp_query = new WP_Query($my_query_args);







while ($wp_query->have_posts()) : $wp_query->the_post(); ?>







<article>



...Some article stuff in here



</article>







<?php endwhile; wp_reset_postdata();?>







<div id="pagination">



<p><span class="left"><?php echo 'Page '. $paged . ' of ' . $wp_query->max_num_pages; ?></span><?php prev_blog_page(); ?><?php next_blog_page(); ?></p>



</div>


Anthony Moore comments:

Thank you that works.