Forums

[resolved] Passing multiple variables into query_posts (41 posts)

  1. nathan12343
    Member
    Posted 2 years ago #

    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

  2. Mark / t31os
    Moderator
    Posted 2 years ago #

    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;
  3. Mark / t31os
    Moderator
    Posted 2 years ago #

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

  4. nathan12343
    Member
    Posted 2 years ago #

    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.

  5. nathan12343
    Member
    Posted 2 years ago #

    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

  6. Mark / t31os
    Moderator
    Posted 2 years ago #

    Get your queried vars from the original 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..

  7. nathan12343
    Member
    Posted 2 years ago #

    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!)

  8. Mark / t31os
    Moderator
    Posted 2 years ago #

    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..

  9. nathan12343
    Member
    Posted 2 years ago #

    GENIUS!!!!

    It works like an absolute charm.

    Thank you, thank you, thank you!

  10. Mark / t31os
    Moderator
    Posted 2 years ago #

    Good stuff, there's also the method used here.
    http://codex.wordpress.org/Template_Tags/query_posts#Preserving_the_Original_Query

    Very similar..

  11. nathan12343
    Member
    Posted 2 years ago #

    ...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!

  12. nathan12343
    Member
    Posted 1 year ago #

    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

  13. Mark / t31os
    Moderator
    Posted 1 year ago #

    Sorry i didn't spot your last response.

    What makes you think it's the array merge failing? A message to that effect?

  14. nathan12343
    Member
    Posted 1 year ago #

    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).

  15. nathan12343
    Member
    Posted 1 year ago #

    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!

  16. Mark / t31os
    Moderator
    Posted 1 year ago #

    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).

  17. nathan12343
    Member
    Posted 1 year ago #

    I'm no expert but it looks like this is the issue....

    <!--<pre>Array
    (
        [meta_key] => end_date
        [meta_compare] => >
        [meta_value] => 2010-06-08
        [orderby] => sac_posts.post_date DESC
        [order] => DESC
        [showposts] => 99
        [page_id] => 7
        [posts_per_page] => 5
        [comments_per_page] => 50
    )
    </pre>-->
  18. Mark / t31os
    Moderator
    Posted 1 year ago #

    Thinking out loud here, but shouldn't the query be setting the orderby to meta_value, so it's ordered by the date set there..

    There's clearly some issue with that start date value anyhow, it's got an extra DESC in it..

  19. nathan12343
    Member
    Posted 1 year ago #

    I can't do anything to get rid of that. I have tried all sorts of different params going into it: 'date', 'meta_value', etc also tried deleting it. But nothing changes the value. When I delete it is comes in to the bottom of the list so I assume this means it is being picked up from WordPress and not from anything in the page.

    The other strange thing is that it doesn't matter if I change 'order' to 'ASC' it still reads 'DESC'. So it would appear that I can't amend order or orderby for these args in this page. I can change these on the get_posts queries on the homepage. Perhaps it is just because this is running from index.php?

    As a work around I guess that I could use the query posts to filter the events according to their end date and then run a get_posts to sort them.

  20. nathan12343
    Member
    Posted 1 year ago #

    this is what I have on the homepage (static page) to show the coming events

    ?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',
            'category_name' => 'Events',
            'order' => 'ASC'
    );
    $events = get_posts($args);
       print '<!--<pre>';
       print_r( $args );
       print '</pre>-->';
    if(is_array($events) && count($events) > 0) :?>
    <?php foreach ($events as $post) :
    setup_postdata($post);
    $start = $my_date = get('start_date');
    $end = $my_date = get('end_date');
    $cost = get_post_meta($post->ID,'price',true);
    if ($end > $str_today )  {
    ?>

    and this is the result of the print:

    <!--<pre>Array
    (
        [meta_key] => end_date
        [meta_compare] => >
        [meta_value] => 2010-06-09
        [orderby] => start_date
        [category_name] => Events
        [order] => ASC
    )
    </pre>-->

    exactly what I want for the query_posts arguement on the what's on pages (index.php)except that 'category_name' should be passed into the query from the page.

  21. Mark / t31os
    Moderator
    Posted 1 year ago #

    Ok have been running some simple tests and i think you were right, there is an issue with the array merge..

    Can you tell me what a typical URL will look like that queries that events page, ie. what parameters are being passed into the URL.. or if you can show me the code that deals with setting up the queries to that page even better (ie. do you have some form of search that submits to the event's page? if so, can i see the code associated so i have a good idea of the query variables you're passing around).

    Once i know that, i can test for it and write something more appropriate for you..

  22. nathan12343
    Member
    Posted 1 year ago #

    At the moment it is simply geared around categories (no site search at the moment...). So the main what's on page is set as the site's blog page (http://www.sidcotartscentre.com/?page_id=7) and then any of the categories under that just pass in the cat id (http://www.sidcotartscentre.com/?cat=3). But I do plan to build a search feature in using drop downs and free text (?s=) I guess - I need more time to be able to play with that!!

    ...is that what you mean?

  23. Mark / t31os
    Moderator
    Posted 1 year ago #

    Aren't both the above two URLs handled by different template files though, presumably the page is handled by page.php and the categories by category.php or index.php depending which exists for your theme?

    As far as i can currently test, the code works without merging the array, which is why i asked about what kind of query variables will be coming into the page, if any... i can write something else for the page, but if there's other variables coming into the page, then i'd rather account for them now than have to re-write the code again.

    So is this page set as your posts page? Or as a front page?

    Just trying to get a little more clarity of how you have things configured..

  24. nathan12343
    Member
    Posted 1 year ago #

    Sorry, I should have explained better. They are both handled by the index.php. Page_id=7 is the what's on page that is set as the posts page in wordpress settings.

  25. Mark / t31os
    Moderator
    Posted 1 year ago #

    Which file are you placing all the aforementioned code into?

  26. nathan12343
    Member
    Posted 1 year ago #

    It is going into index.php

  27. Mark / t31os
    Moderator
    Posted 1 year ago #

    Right i think understand how you're using the code, and where, etc.

    Just one more question...
    So you're using a page both as the posts page and as an events page, would it not be easier to have two seperate pages (one for regular posts, one for events)?

    Or are you not using posts in the traditional sense? (ie. you have no need for regular posts)

  28. nathan12343
    Member
    Posted 1 year ago #

    At the moments the only posts are events (although I have now been asked to included press information which will probably form their own category of posts). So yes at the moment only posts are events.

  29. Mark / t31os
    Moderator
    Posted 1 year ago #

    Perhaps a conditional statement to check if the events page has been queried would be sufficient.

    if( is_page('7') ) {
    
        // YOUR CODE
    
    }

    The index.php should then only run that query when viewing the events page.

    You'd then not have to worry about preserving query vars for other views, ?cat=x, ?tag=x etc..

  30. nathan12343
    Member
    Posted 1 year ago #

    Again, sorry, it doesn't look like I've explained it well at all!

    The what's on page shows all of the events (currently the only posts). These event post are in a category called 'events'. Underneath this there are subcategories or 'exhibitions', 'classes', 'family activities', etc. The menu shows this second layer of category so that the user can click to find specifically music events. In the same way the posts are tagged so that a user could find similar events. This was why I used the main index.php to filter according to the end_date > now so that for all of these types of navigation I only showed events that hadn't finished.

Topic Closed

This topic has been closed to new replies.

About this Topic