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
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
Related Q & A:
- How can I change a value in an array?Best solution by Stack Overflow
- How can I change the language of the menus interface in the group I'm moderating?Best solution by Yahoo! Answers
- How can I change the font for e-mail that I compose?Best solution by Yahoo! Answers
- HOW CAN I CHANGE MY NAME FROM MY ACCOUNT I MADE A MISTAKE?Best solution by Yahoo! Answers
- How do I change the order of the songs on my iTunes playlist?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.