How can you change the order of your posts?

How can I change the order of the displayed posts according to meta_value and title?

  • my website is about poetry. I use a custom field for each poem where the meta key is "Author" and the meta value the author's name, e.g. "Allison, James". Now I'd like my posts to show up in the loop in a certain order: alphabetically according to the meta value mentioned above (e.g., all poems by "Allison, James"; then all poems by "Barnes, John"; then all poems by "Carter, Sue" and so on). The poems of one specific author (= meta value) should then be ordered alphabetically by title (e.g. Allison, James: "After dark", "Before sunset" and so on). Is there a way to do this? I've found the string below, but it wouldn't work (perhaps I put it in the wrong place or I have to add some code somewhere else in addition to make it work). Do you have an idea what piece of code I have to put in which php-file to get the desired effect? <?php $querystr = " SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'Autor' AND wposts.post_type = 'post' ORDER BY wpostmeta.meta_value ASC "; $pageposts = $wpdb->get_results($querystr, OBJECT); ?> Thanks a lot! Cheers, Felix

  • Answer:

    What about: SELECT DISTINCT $wpdb->posts.*, meta_value AS author FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE 1=1 AND post_status = 'publish' AND post_type = 'post' AND meta_key = 'Autor' ND post_password = '' ORDER BY author DESC;

Felix Weigand at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

thanks for your idea! However, I heard it might be better to use the query function. I found different examples in the WP codex (http://codex.wordpress.org/Function_Reference/query_posts), but don't know how to put them together into one single piece of code - maybe you can help? query_posts( array ( 'post_type' => 'post', 'orderby' => 'meta_value', 'meta_key' => 'Autor' ) ); Here should be added something... ...to call only published posts ...to order the meta_values (= author's names) as well as the titles that belong to a certain meta_value alphabetically and ascending - 'orderby=title&order=ASC' ...to prevent that there are all posts listed vertically one after another (on my website there is only one post per page - you click through the texts horizontally). Perhaps this order will do: 'posts_per_page=1'... In my theme, there are three files containing loop elements (index.php, single.php, inc_head.php) - in which of those files do I need to insert the query function? (I'll post the loop code for these files which is not working right now...) Thanks a lot! Regards, Felix

Felix Weigand

Here we go - this is the loop code of my theme (I don't know how to dosplay code here): http://dl.dropbox.com/u/18048832/Code/file%20code.txt

Felix Weigand

I finally found asolution :o) Using the Ambrosite Plugin, I added the following pieces of code into my single.php: <?php previous_post_link_plus( array( 'loop' => true, 'link' => '«', 'format' => '%link', 'order_by' => 'custom', 'order_2nd' => 'post_title', 'meta_key' => 'Autor', 'order'=>'ASC', 'in_same_cat' => true ) ); ?> and: <?php next_post_link_plus( array( 'loop' => true, 'link' => '»', 'format' => '%link', 'order_by' => 'custom', 'order_2nd' => 'post_title', 'meta_key' => 'Autor', 'order'=>'ASC', 'in_same_cat' => true ) ); ?> It works great! Thanks anyway, Felix

Felix

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.