Title: Passing multiple variables into query_posts
Last modified: August 19, 2016

---

# Passing multiple variables into query_posts

 *  Resolved [nathan12343](https://wordpress.org/support/users/nathan12343/)
 * (@nathan12343)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/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

Viewing 15 replies - 1 through 15 (of 40 total)

1 [2](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/page/2/?output_format=md)

 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300513)
 * You’re combining the args incorrectly..
 *     ```
       $query_string. '&' .$args
       ```
   
 * is 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;
       ```
   
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300515)
 * 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) );?>`
 *  Thread Starter [nathan12343](https://wordpress.org/support/users/nathan12343/)
 * (@nathan12343)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300559)
 * 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_string` gives me _page\_id=7_ which is my blog homepage (I have 
   a static front page)
 * But query_posts is still ignoring $args completely.
 *  Thread Starter [nathan12343](https://wordpress.org/support/users/nathan12343/)
 * (@nathan12343)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300564)
 * 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’t
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300578)
 * Get your queried vars from the original query.
    [http://codex.wordpress.org/Function_Reference/WP_Query](http://codex.wordpress.org/Function_Reference/WP_Query)`
   $query` or `$query_vars`
 * Merge that with your own array, then pass that array into query_posts..
 *  Thread Starter [nathan12343](https://wordpress.org/support/users/nathan12343/)
 * (@nathan12343)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300633)
 * 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!)
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300698)
 * 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_filter` removes any empty/false/null values from the query_vars array
   when merging..
 *  Thread Starter [nathan12343](https://wordpress.org/support/users/nathan12343/)
 * (@nathan12343)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300728)
 * GENIUS!!!!
 * It works like an absolute charm.
 * Thank you, thank you, thank you!
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300734)
 * Good stuff, there’s also the method used here.
    [http://codex.wordpress.org/Template_Tags/query_posts#Preserving_the_Original_Query](http://codex.wordpress.org/Template_Tags/query_posts#Preserving_the_Original_Query)
 * Very similar..
 *  Thread Starter [nathan12343](https://wordpress.org/support/users/nathan12343/)
 * (@nathan12343)
 * [16 years ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300891)
 * …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!
 *  Thread Starter [nathan12343](https://wordpress.org/support/users/nathan12343/)
 * (@nathan12343)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300894)
 * 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
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300895)
 * Sorry i didn’t spot your last response.
 * What makes you think it’s the array merge failing? A message to that effect?
 *  Thread Starter [nathan12343](https://wordpress.org/support/users/nathan12343/)
 * (@nathan12343)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300896)
 * 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](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).
 *  Thread Starter [nathan12343](https://wordpress.org/support/users/nathan12343/)
 * (@nathan12343)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300897)
 * 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 &raquo;</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!
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/#post-1300898)
 * 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).

Viewing 15 replies - 1 through 15 (of 40 total)

1 [2](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/page/2/?output_format=md)

The topic ‘Passing multiple variables into query_posts’ is closed to new replies.

## Tags

 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 40 replies
 * 2 participants
 * Last reply from: [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * Last activity: [15 years, 10 months ago](https://wordpress.org/support/topic/passing-multiple-variables-into-query_posts/page/3/#post-1300927)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
