Hi there,

I am in need of help, I would like to know how to create a link that when clicked it will change the order a list is displayed.

Specifically in wordpress when using a codex to display posts.

How would I create a link or dropmenu that can sort the post in different way.

So for example default could be:


Then user selects "sort by date" link and the page loads this php:



If someone might know also (but I can ask this in wordpress forums) how can I display the "date, author, comment, etc" in a table form when the query creates the post list (so instead of showing just excerpt & title - it shows 1)title 2)date 3)author 4) amount of comments) I assume I use get_posts instead.

Recommended Answers

All 6 Replies

Just thought I would post the solution for the last part of my question in case someone down the road needs to know. This is basically it.

 $postslist = get_posts('order=ASC&orderby=title');
 foreach ($postslist as $post) :
 <td><?php the_title(); ?></td>
 <td><?php the_excerpt(); ?></td>
 <td><?php the_date(); ?></td>
<td><?php the_rating(); ?></td>
<td><?php the_author(); ?></td>
<td><?php the_category(); ?></td>
 <?php endforeach; ?>

ie. (displays)
Title | Excerpt | Date | Rating | Author | Category

So back to my first question, how would I create a link that would change "title" to "date" or change to use new php script (orderby=title)?

So when someone clicks on say "Rating" it will sort by rating. (just wanted to clear it up if I didn't make sense)

Umm, sorry I don't understand. Is your problem solved or do you still have a question?

Opps, guess I should of just stayed quiet, but yes I still am looking for an answer.

I have some code I have been playing with.

<?php $sort= $_GET['sort'];

"Want to set a default value (ie. orderby=date) here to load first"

if($sort == "A")
{$order= "orderby=title";}
if($sort == "B")
{$order= "orderby=author";}

<?php $updown= $_GET['updown'];

"Want to set a default value (ie. ASC) here to load first"

if($updown == "D")
{$ascend= "ASC";} //this orders in ascending
if($updown == "C")
{$ascend= "DEC";} //this orders in descending

Links for visitor to click:

<a href="?sort=A">title</a>
<a href="?sort=B">author</a>
//when someone clicks on A or B then clicks on C would like to know how to keep the value for A or B selected
<a href="?sort=D">Ascending</a>
<a href="?sort=C">Descending</a>

PHP (wordpress codex) to list post:

<?php query_posts($order.'&order=$.ascend.$category_id=$cat.');  ?>
<?php $sortcat= $_GET['sortcat'];

"would like a default value of say all categorys (20 & 21)"

if($sortcat== "c1")
{$cat= "20";}
if($cat== "c2")
{$order= "21";}
<a href="?sort=c1">Category Movies</a>
<a href="?sort=c2">Category Games</a>

So if someone clicks on say "order by title" it will order the list by title, but than say they click on category "movies" it will only list the post belonging to movies category, but will it keep the order by title listed or change back to default.

The easiest way to do that would be pass back the sort variable to the view (If it's available).

Since you already capture the sort variable earlier on in the script you can pass it on to the view. I suggest you change your code to capture it like so,

$sort= isset($_GET['sort']) ? $_GET['sort'] : "A";
//I've set the default to A, change it if you like

Just pass it to the view like so,

<a href="?updown=D&sort=<?php echo $sort?>">Ascending</a>
<a href="?updown=C&sort=<?php echo $sort?>">Descending</a>

* I'm assuming you actually meant updown for the Ascending and Descending links as that's what your PHP code indicates.

You can change the other links like this as well so that the current state is always passed to the PHP script.

I'm not sure whether this is exactly what you're looking for, so let me know if I've gone off on a completely wrong tangent. Might take some time for a reply though as I might be incommunicado for the rest of the week.

Wow, this is great, thanks alot for the help.

I will play around with for a while and see what I can get done.


Hey! Did you manage to solve this? I am using your solution right now and see if this works.

I've been looking around for about a week now and nothing seems to work so I am having high hopes with your solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.