Is there any way of changing the post order via user click?

Is there any way of changing the post order, posts number, etc via user click?

The same way you can choose displaying questions by time and vote here at StackExchange.

Read More

How to accomplish that?

Related posts

Leave a Reply

5 comments

  1. By using the $_GET function at the top

    <?php $sort= $_GET['sort'];
    if($sort == "title")
    {
    $order= "orderby=title";
    }
    if($sort == "date")
    {
    $order= "orderby=date";
    }
    ?>

    You can use links like that:

    <a  href="?sort=title" <?php if ($sort == "title"){ echo 'style="color:gray"'; } ?>>title</a><a href="?sort=date" <?php if ($sort == "date"){ echo 'style="color:gray"'; } ?>>Date</a>
    

    Or a dropdown list:

    <form action="" method="get">
    <select name="sort" id="sorting">
      <option value="title" <?php if ($sort == "title"){ echo 'selected="selected"'; } ?> >Sort by title</option>
      <option value="date" <?php if ($sort == "date"){ echo 'selected="selected"'; }?> >Sort by publication date</option>
    </select>
    <input type="submit" value="Submit" /></form>
    

    Then Comes the loop

    <?php $loop = new WP_Query('cat=5&showposts=-1&'.$order.'&order=DEC'); ?><?php while ( $loop->have_posts() ) : $loop->the_post(); ?>.........<?php endwhile; ?>
    
  2. Just my example based on Boldhand a way to change the title of a Custom Post Type ASC and DESC:

              <?php 
                    $sort= esc_attr($_GET['sort']); 
                    if($sort == "title") { $order= "orderby=title&order=DESC"; } 
                    if($sort == "titleb") { $order= "orderby=title&order=ASC"; } 
                    ?>
    
                    <div class="elementsToFilter">
                      <ul>
                        <li><a  href="?sort=title" <?php if ($sort == "title"){ echo 'style="color:gray"'; } ?>>Title : A - Z</a></li>
                        <li><a href="?sort=titleb" <?php if ($sort == "titleb"){ echo 'style="color:gray"'; } ?>>Title : Z - A</a></li>
                      </ul>
                    </div>
    
    
                <div id="containerBoxes">
    
                    <?php $loop = new WP_Query('post_type=portfolio&posts_per_page=12'.$order); ?><?php while ( $loop->have_posts() ) : $loop->the_post(); ?>       
                                  <div class="box">
                                    <h4><?php the_title() ?></h4>
                                    <?php the_content() ?>
                                  </div>
    
    
                     <?php endwhile; wp_reset_query(); ?> 
    
               </div> <!-- / containerBoxes-->