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

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Combine Custom Sorting Filters

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>

This question has been answered.

Anthony Moore | 01/18/13 at 9:11pm Edit
Tutorial: How to assign prize money


(6) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    01/18/13
    9:28pm
    Arnav Joy says:

    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>

  • avatar
    Last edited:
    01/19/13
    5:25pm
    Anthony Moore says:

    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"?

  • avatar
    Last edited:
    01/19/13
    5:35pm
    Arnav Joy says:

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

  • avatar
    Last edited:
    01/19/13
    5:43pm
    Anthony Moore says:

    Currently it does not work.

  • avatar
    Last edited:
    01/19/13
    7:51pm
    Arnav Joy says:

    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>

  • avatar
    Last edited:
    01/21/13
    7:55pm
    Anthony Moore says:

    Thank you that works.

This question has expired.



Anthony Moore voted on this question.



Current status of this question: Completed



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.