Passing multiple variables into query_posts
-
Hi,
I am trying to filter the posts in the loop according to a filter BUT at the same time I want to preserve the query in the URL. The posts have a filter of end_date and I only want to show posts where the end date haven’t passed already.
The filter works with the filter on the meta_value ($args) but I can’t get this to work and preserve the original query.
<?php global $query_string; $my_now = date('Y-m-d'); $start = $end =''; $end = $my_date = get('end_date'); $args = array( 'meta_key' => 'end_date', 'meta_compare' => '>', 'meta_value' => $my_now, 'orderby' => 'start_date', 'order' => 'des', 'showposts' => '99', ); $whatsonposts = query_posts($query_string. '&' .$args);?>Anyone know where I’m going wrong??
Thanks
-
You’re combining the args incorrectly..
$query_string. '&' .$argsis basically saying..
array() $string array()where $string is
&…Try.
$args = array( 'meta_key' => 'end_date', 'meta_compare' => '>', 'meta_value' => $my_now, 'orderby' => 'start_date', 'order' => 'des', 'showposts' => '99', ); $args = array_merge( $query_string , $args ); $whatsonposts = query_posts( $args );?>…failing that, instead of.
$args = array_merge( $query_string , $args );try…
$args = $query_string+$args;Thinking about it though, query_string is likely a string (hence the name, d’oh!)…
You could also try…
$whatsonposts = query_posts( $query_string .'&'. implode('&',$args) );?>Thanks for your suggestions.
I tried the different routes, but none worked. On the upside it looks like it is getting closer!
With a bit of a play using your code I did this:
$args = implode('&',$args); $whatsonposts = query_posts($query_string.'&'.$args); echo ($args); ?>echo threw back this: end_date&>&2009-12-07&start_date&des&99 so it looks to me like this is along the right lines (but where have the param names gone?).
echo $query_stringgives me page_id=7 which is my blog homepage (I have a static front page)But query_posts is still ignoring $args completely.
I can’t help but think the missing field names is everything (but then what do I know!!)
echo ($query_string.'&'.$args); ?>returns the following:
page_id=7&end_date&>&2009-12-07&start_date&des&99
I think that there is something happening with the implode function. because
$whatsonposts = query_posts($args);works to filter the posts, but$args = implode('&',$args); $whatsonposts = query_posts($args);doesn’tGet your queried vars from the original query.
http://codex.wordpress.org/Function_Reference/WP_Query
$queryor$query_varsMerge that with your own array, then pass that array into query_posts..
Sorry, but this is well beyond my techie limit! If you get a moment I’d really appreciate it if you would be able to show me how to go about this (I’ve litterally not got a clue where to start!)
Try this for the args array..
<?php $args = array( 'meta_key' => 'end_date', 'meta_compare' => '>', 'meta_value' => $my_now, 'orderby' => 'start_date', 'order' => 'desc', 'showposts' => '99' ); $args = array_merge( $args , array_filter( $wp_query->query_vars ) ); ?>The
array_filterremoves any empty/false/null values from the query_vars array when merging..GENIUS!!!!
It works like an absolute charm.
Thank you, thank you, thank you!
Good stuff, there’s also the method used here.
http://codex.wordpress.org/Template_Tags/query_posts#Preserving_the_Original_QueryVery similar..
…I hate to resurrect this but I’ve noticed this either isn’t working (since a WP upgrade perhaps) or didn’t in the first place, but I didn’t spot it. The problem is that it isn’t being ordered by the date. These params work elsewhere when I am using them with get_posts but it looks like it fails with the array_merge line…
t31os help me!
Is there anyone out there with any ideas where this might be going wrong? I really need to get it working for an events page – which is why i need to filter out anything that has past its end date but sort it so that the most recent event is at the top.
Thanks
Sorry i didn’t spot your last response.
What makes you think it’s the array merge failing? A message to that effect?
Hi! No problem, thanks for getting back.
The reason it looks to be failing is that the actual page doesn’t display according to the start date of the event.
http://www.sidcotartscentre.com/?page_id=7
But the code behind this page is exactly as above to order the filtered results according to the start date. What is actually happening is that the results are shown by newest posted first.
The filtering of events that have already ended is working fine. And I know that the start date custom field is populated properly because I can use it to sort outside of this (like on the homepage).
This is the code as it appears in all its glory:
<?php $now = strtotime('now'); $str_today = date('m/d/Y'); $my_now = date('Y-m-d'); $start = $end =''; $end = $my_date = get('end_date'); $args = array( 'meta_key' => 'end_date', 'meta_compare' => '>', 'meta_value' => $my_now, 'orderby' => 'start_date', 'order' => 'desc', 'showposts' => '99' ); $args = array_merge( $args , array_filter( $wp_query->query_vars ) ); $whatsonposts = query_posts($args); ?> <div id="main_content"> <?php if(is_array($whatsonposts) && count($whatsonposts) > 0) {?> <?php if (have_posts()): ?> <!--str_today: <?php echo $str_today ?> <br /> My now: <?php echo $my_now ?> <br />--> <?php while (have_posts()) : the_post(); ?> <?php $start = $my_date = get('start_date'); $end = $my_date = get('end_date'); $cost = get_post_meta($post->ID,'price',true); ?> <div <?php post_class('post') ?>> <div class="post-header" id="post-<?php the_ID(); ?>"> <div class="post-name"> <h1><a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h1> </div> <div class="post-detail"><?php comments_popup_link('No Comments', '1 Comment', '%Comments'); ?><?php edit_post_link('Edit', ' - [', ']'); ?></h3></div> </div> <div class="post-body"> <?php if($start !='') echo date('F j, Y',strtotime($start)); if($end !='') echo ' - '.date('F j, Y',strtotime($end));?><br /> Price: <?php echo $cost; ?> <br /> <?php the_excerpt('<p class="serif">Read the rest of this entry »</p>'); ?> <a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">[Click here for more]</a> <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?> </div> </div> <?php endwhile; endif; ?> <?php } else { ?> <?php include 'sorrymsg.php' ?> <?php } ?>Thanks!
Let’s check the args are correct first then.. after this line
$args = array_merge( $args , array_filter( $wp_query->query_vars ) );Add..
print '<!--<pre>'; print_r( $args ); print '</pre>-->';Then load the page and look in the source code for the output (since it’s wrapped in html comments you’ll see nothing extra onscreen).
The topic ‘Passing multiple variables into query_posts’ is closed to new replies.