Question

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.

How to accomplish that?

Was it helpful?

Solution

Check the WP Sort Plugin, it does exactly what you need:

http://wordpress.org/extend/plugins/wp-post-sorting/

OTHER TIPS

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

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

Yes, it is possible. You'll want to look at the Order & Orderby Parameters to WP_Query and since you didn't give too many details here, you should probably also look at When should you use WP_Query vs query_posts() vs get_posts()? as well.

You can always add orderby to your URLs to specify a field to order the posts. Earlier today I gave an answer on how to do this with "nice" URLs.

So you ("only") need to generate the links on each archive page. This should be rather easy, but I think you can find plugins for that too.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top