Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a template called taxonomy-book_style.php to handle all the terms in book_style. On it I just have a simple loop

<?php if (have_posts()) : while (have_posts()) : the_post();?>

I would like to order my posts by the meta key value found in the meta_key tf_book_sort

So far I've tried these but they do not work:

<?php 
query_posts( $query_string . 'orderby=meta_value&meta_key=tf_book_sort&order=ASC' );?>
<?php if (have_posts()) : while (have_posts()) : the_post();?>

<?php if (have_posts()) : while (have_posts()) : the_post('meta_key=tf_book_sort&orderby=meta_value&order=ASC'); ?>

<?php 
$args = array(
'tax_query' => array(array('taxonomy' => 'book_style')),
'meta_key' => 'tf_book_sort',
'orderby' => 'meta_value',
'order' => 'DESC'
);?>
<?php query_posts ($args);?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

The nested arrays are confusing, can someone please suggest a proper synatx to accomplish sorting these posts by a meta key value?

Thank You.

share|improve this question
add comment

1 Answer

first of all, let's just forget query_posts exists. there's no good reason to use it, ever.

the best way to do this is via the pre_get_posts action in your theme's functions.php. you don't have to set any taxonomy parameters, those are already set on a taxonomy archive page, you just need meta key and orderby:

function wpa65258_tax_meta_orderby( $query ) {
    if ( is_tax( 'book_style' ) && is_main_query() ) : // edit -> added is_main_query()
        $query->set( 'meta_key', 'tf_book_sort' );
        $query->set( 'orderby', 'meta_value_num' );
    endif;
}
add_action( 'pre_get_posts', 'wpa65258_tax_meta_orderby' );

This assumes your meta value is numeric. if it's not, change orderby to meta_value instead of meta_value_num.

Then in your template, just do the normal loop:

if (have_posts()) : while (have_posts()) : the_post();

etc..

share|improve this answer
    
Hmm, that still gives me nothing. It also strangely causes all my post_types to disappear from the front end. –  Nadine Sep 16 '12 at 2:02
    
I tested and it's working in TwentyEleven with the register taxonomy and post type code from Codex. You likely have an issue with some other code in your theme or a plugin. –  Milo Sep 16 '12 at 2:26
    
Is there a solution using query posts? It dosen't have to be pretty. –  Nadine Sep 16 '12 at 4:05
    
a better solution is to learn to debug your queries and figure out where your errors are. –  Milo Sep 16 '12 at 17:02
    
Debug only returned two sidebar errors, which I fixed. Function still gives me no listings, and still removes my post_types. Its like its over-riding the normal loop on that template for some reason. Thanks for your response and the link to that plugin. –  Nadine Sep 17 '12 at 0:28
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.