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 custom post type where I have a taxonomy cities and a term berlin. Archive can be seen at example.com/cities/berlin and it's rendered normally from the archive.php template.

Now, I want to order them according to a custom field, start_date in this case, but still keep the original query around. So that when I navigate to example.com/cities/newyork the loop would still work as expected.

This is what I have but I only get the <p>Not Found</p> in the end.

Please help.

Thanks :)

<?php
global $query_string; //to keep the original query around
$args = array(
  'orderby'  => 'meta_value_num',
  'meta_key' => 'start_date',
  'order'    => 'DESC'
);
$posts = query_posts($query_string . $args);
if (have_posts()) : while (have_posts()) : the_post(); 
?>

do stuff

<?php endwhile; else : ?>
    <p>Not Found</p>
<?php endif; ?>
share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

You can't concatenate an array onto query_string like that. See Digging Into WordPress's post about looping for the right format. Alternately, you can use array_merge() like on the query_posts Codex page.

Consider using wp_reset_query() after your loop. Sometimes you'll also see people save the original $query_string to a new variable, modify $query_string, and then reset $query_string after the loop like so:

global $query_string;
$old_query = $query_string
// Modify your query, loop through it
$query_string = $old_query;
share|improve this answer
    
Thanks I'll try that :) –  any_h Apr 17 '12 at 22:18
    
@any_h if this solved your problem, make sure to accept it as the answer. Otherwise, update your question and explain why this didn't work. –  mrwweb May 8 '12 at 19:19
add comment

Old question, but it was on the front page, so it is my duty to post and say "Don't ever use query_posts()" To modify the query without destroying the original query, you should use the pre_get_posts hook.

function wpa_49346_pre_get_posts( $query ){
    if( is_tax() ){
        set_query_var( 'meta_key', 'start_date' );
        set_query_var( 'orderby', 'meta_value_num' );
        set_query_var( 'order', 'DESC' );
    }
}
add_action( 'pre_get_posts', 'wpa_49346_pre_get_posts' );
share|improve this answer
add comment

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.