Leave a Reply

1 comment

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