3

I have this include … event-list.php

<?php
/**
 * The loop that displays upcoming events
 */
?>

<ul class="event-items">
<?php

    $yesterday = time() - 24*60*60;
    $args = array( 
        'post_type' => 'wr_event', 
        'posts_per_page' => -1, // show all posts 
        'meta_key' => 'event_date', 
        'orderby' => 'meta_value_num', 
        'order' => 'ASC',
        'meta_value' => $yesterday,
        'meta_compare' => '>'           
    );

    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        get_template_part( 'inc/event', 'item' );
    endwhile;
    wp_reset_postdata();
?>
</ul>

As you can see it's a custom query that compares a meta_key with the current date and only retrieves posts that are "newer" than yesterday.

however in my archives I want to do it the other way around.

On my index.php I have this

<?php get_template_part( 'inc/event', 'list' ); ?>

So this template above is getting executed and only retrieves events that are fresher than the last 24hours.

In my archives.php though I'd like to reverse the meta_compare value from ">" to "<" so I get the rest of the posts.

Is there a clever way of doing this?

So in my archives.php I have this as well …

<?php get_template_part( 'inc/event', 'list' ); ?>

Can I somehow pass the 'meta_compare' value to this template? So I can have and use the same template file in my index.php and in my archives.php but with different 'meta_compare' values.

Any ideas on that?

3
+50

Why don't you use a simple function with an argument to achieve that, the code is something like this:

function wpse63585_event_list( $fresh = true )
{
    echo '<ul class="event-items">';

    $yesterday = time() - 24*60*60;
    $compare = $fresh ? '>' : '<';

    $args = array(
        'post_type' => 'wr_event',
        'posts_per_page' => -1, // show all posts
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        'meta_value' => $yesterday,
        'meta_compare' => $compare,
    );

    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        get_template_part( 'inc/event', 'item' );
    endwhile;
    wp_reset_postdata();
    echo '</ul>';
}

And then in your index.php, just call:

<?php wpse63585_event_list(); ?>

and in archives.php:

<?php wpse63585_event_list( false ); ?>

Another solution I can think about is you can register a global variable, like $wpse63585_fresh = true, and use that variable in your template part event-list.php to control the condition, like this:

In index.php:

global $wpse63585_fresh;

$wpse63585_fresh = true;
get_template_part( 'inc/event', 'list' );

In archive.php:

global $wpse63585_fresh;

$wpse63585_fresh = false;
get_template_part( 'inc/event', 'list' );

In event-list.php:

<ul class="event-items">
<?php
    global $wpse63585_fresh;
    $compare = $wpse63585_fresh ? '>' : '<';

    $yesterday = time() - 24*60*60;
    $args = array( 
        'post_type' => 'wr_event', 
        'posts_per_page' => -1, // show all posts 
        'meta_key' => 'event_date', 
        'orderby' => 'meta_value_num', 
        'order' => 'ASC',
        'meta_value' => $yesterday,
        'meta_compare' => $compare           
    );

    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        get_template_part( 'inc/event', 'item' );
    endwhile;
    wp_reset_postdata();
?>
</ul>
  • Thank you, couldn't come up with this easy solution. I'll award you with the +50 as soon as it's possible. It says I have to wait for 22hours. Thank you again. – mathiregister Sep 1 '12 at 8:58
  • 2
    But still, there are many cases requiring variable passed to the get_template_part(). – BasTaller Sep 30 '13 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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