0

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; ?>

2 Answers 2

2

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;
1
  • @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, 2012 at 19:19
0

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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