• Hello, I am comparing dates in php, to display only upcoming events (posts with special start_date and end_date custom fields to which I already applied the strtotime() function). I would like to show the past events ONLY if no upcoming events exist. Do you think this will work? My concern is specifically the elseif clause, since the one below doesn’t seem to work.

    if ($date_start >= $today || $date_end >= $today) :
    // show upcoming event(s)
    elseif(($date_start >= $today) == false || ($date_end >= $today) == false):
    // show past event(s)
    endif;
Viewing 7 replies - 1 through 7 (of 7 total)
  • J M

    (@hiphopinenglish)

    Where are you using this code? What does it currently do (ie what kind of output are you getting)?

    Thread Starter mad_griffith

    (@niccolomineo)

    I am currently using it without the elseif, in index.php and in archive.php, within the Loop. The output are normal posts that I turned into events by creating custom fields for them ($date_start and $date_end). They show up because they are considered upcoming, as I wrote in the if clause.

    J M

    (@hiphopinenglish)

    Why don’t you test with a simple else clause? (Rather than elseif). This will help you see if your if clause is working correctly. Remember you can check any variables using print_r($variable); which helps.

    Thread Starter mad_griffith

    (@niccolomineo)

    it actually prints only past events, but it prints them EVEN THOUGH THERE ARE upcoming events. I want the past events to show up ONLY WHEN THERE ARE NO upcoming events.

    EDIT: I also have events in subcategories. I noticed that the past events are printed not by date but by subcategory. So, if an event is more recent than another event, but belongs to a subcategory that has a higher index in the categories array, this most recent event will appear as less recent (= retrieved afterwards from the DB). Probably this can be solved by going back to the original problem: I would need to write an appropriate elseif, that I don’t know how to write.

    J M

    (@hiphopinenglish)

    I think the issue is that you’re in the loop, and the loop is set to return all events. Your if elseif statement is simply filtering the events according to which criteria they meet. Unless I’ve completely misunderstood I think the issue lies in what your WP_Query loop is returning.

    Thread Starter mad_griffith

    (@niccolomineo)

    You are probably right, but I wouldn’t know how to do the changes. Can you help?

    This is what I am currently using to query the articles:

    <?php query_posts('category_name=events&posts_per_page=8'); ?>

    “Events” is the categories, which has several subcategories where the events actually belong to.

    J M

    (@hiphopinenglish)

    OK. For a start it’s certainly not recommended to use query_posts as a quick Google will tell you.

    I’d recommend using a full-blown WP_Query with a nice loop.

    $args = array (
    						'post_type'              => 'events',
    						'post_status'            => 'publish',
    						'pagination'             => false,
    						'posts_per_page'         => '-1',
    						'order'                  => 'ASC',
    						'meta_key'               => '_iqbru_zig_zag',
    						'orderby'                => 'meta_value',
    						'meta_query' => array(
    						'relation' => 'AND',
    							array(
    								'key' => '_start_date',
    								'value' => $today,
    								'compare' => '<=',
    								'type' => 'NUMERIC'
    							),
    							array(
    								'key' => '_end_date',
    								'value' => $today,
    								'compare' => '>=',
    								'type' => 'NUMERIC'
    							),
    						),
    					);
    
    					// The Query
    					$today_groups = new WP_Query( $args );
    
    					// The Loop
    					if ( $today_groups->have_posts() ) {
    while ( $today_groups->have_posts() ) {
    							$today_groups->the_post();

    Something like that to get you started.
    Note that I have not closed the loop, and that you can always use the if have_posts to close with a elseif to grab your past events if no future events were found.

    You’ll need to play with this, but hopefully it helps get you started.

    *Apologies for the terrible formatting.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display past events only if no upcoming events exist’ is closed to new replies.