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?
Why don’t you use a simple function with an argument to achieve that, the code is something like this:
And then in your
index.php
, just call:and in
archives.php
:Another solution I can think about is you can register a global variable, like
$wpse63585_fresh = true
, and use that variable in your template partevent-list.php
to control the condition, like this:In
index.php
:In
archive.php
:In
event-list.php
: