0

I need to be able to display a list of events on 1 page. The top list would be for upcoming events and to be sorted with earliest event first and the second list would be for expired/past events and be sorted with the latest event on top.

Example:
Upcoming event

  • Event A • July 30, 2015
  • Event Z • September 2, 2015
  • Event G • December 9, 2015

Past Events

  • Event F • June 29, 2015
  • Event X • May 5, 2015
  • Event D • February , 2015


I can't figure out how to change the orderby value from DESC to ASC within the IF statement that checks if end date is expired.

My code is as follow:

//GET THE CURRENT DATE
$current_date = date('F j, Y');
$current_date = strtotime( $current_date );

$args = array(      
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post_type'        => 'event',
    'post_status'      => 'publish',
    'suppress_filters' => true
);

$posts = get_posts( $args );

// BUILD SHORTCODE CODE TO BE INSERTED
foreach($posts as $post){       

    $event_start_date=get_post_meta($post->ID,'event-start-date');
    $event_end_date=get_post_meta($post->ID,'event-end-date');

    $event_start=date('F j, Y', $event_start_date[0]);
    $event_end=date('F j, Y', $event_end_date[0]);
    $exp_date = strtotime( $event_end );

    if( $exp_date > $current_date ) {

    $shortcode_str="[MY_SHORTCODE_CURRENT]";
    } 
    else {
    $shortcode_str_ex="[MY_SHORTCODE_EXPIRED]";
    }
}
0

You can't change the DESC to ASC "within the IF statement". The query has already ran at that point. I don't really understand what the code is meant to do though. I don't really see that code ever producing the pattern you describe. Specifically, I don't understand what the shortcode strings or for or how that is supposed to work. However, I can duplicate the result. Maybe that will help.

//GET THE CURRENT DATE
$current_date = date('F j, Y');
$current_date = strtotime( $current_date );

$args = array(      
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'suppress_filters' => true
);

$posts = get_posts( $args );

$expired = array();

// BUILD SHORTCODE CODE TO BE INSERTED
foreach($posts as $post){       
  $event_start_date=get_post_meta($post->ID,'event-start-date',true);
  $event_end_date=get_post_meta($post->ID,'event-end-date',true);

  if (empty($event_start_date) || empty($event_end_date)) continue;
  $event_start=date('F j, Y', $event_start_date);
  $event_end=date('F j, Y', $event_end_date);
  $exp_date = strtotime( $event_end );

  echo '$exp_date > $current_date<br>';
  if( $exp_date > $current_date ) {
    // Match:
    // Event A • July 30, 2015 
    echo the_title(),' &bull; ',$event_end;
    echo '<br>';
  } 
  else {
    array_unshift($expired, get_the_title().' &bull; '.$event_end);
  }
}
echo '$expired<br>';
if (!empty($expired)) {
  echo implode('<br>',$expired);
}

Note: Without going through a bunch of posts and setting the meta fields, which would be tedious, I can't test this but I have a fair certainty that that is on the right track.

Edit: I did clean up the code a little but it essentially does work. array_unshift prepends items to an array so if the original items are sorted DESC then the array created by array_unshift should be the reverse order.

  • Thanks for the attempt, but the code doesn't seem to take in account the "sorting" part? – ogmios Jul 10 '15 at 5:46
  • Ok. You are going to force me to do the tedious :) – s_ha_dum Jul 10 '15 at 14:01
  • The second sorting is done by array_unshift. See the edit. – s_ha_dum Jul 10 '15 at 14:19
  • Yes, very sorry, I didn't get that. I will try this approach and let you know! – ogmios Jul 10 '15 at 15:52

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.