Support

Home Forums Event Espresso Premium Using EE_Event_List_Query to sort events by start date instead of post date

Using EE_Event_List_Query to sort events by start date instead of post date

Posted: August 30, 2016 at 2:41 pm


yamamDev

August 30, 2016 at 2:41 pm

Hello,

Im trying to use the EE_Event_List_Query to print out the events by their event start date instead of using WP_query to print events by their creation date. Im open to using either though.

Ive tried to model my loop after Josh’s example, but im having an issue accessing the post object. Heres what ive got so far:

$eventAtts = array(
    'limit' => -1,
    'order_by' => 'start_date',
    'sort' => 'ASC',
    'category_slug' => $taxonomySlug,
    'post_status' => 'publish',
    'show_expired' => FALSE
  );
  global $events_query;
  $events_query = new EE_Event_List_Query( $eventAtts );
  if (have_posts()) : while (have_posts()) : the_post();

    $event_obj = $post->EE_Event;
    $date_times = $event_obj->first_datetime();

  endwhile;
  endif;

Im having an error saying:

Fatal error: Call to a member function get_permalink() on a non-object

How can i access the event object in the events_query?


yamamDev

August 30, 2016 at 2:45 pm

My end goal is to return the events start date in a certain format (August 2016). I had this working before, but it was sorting by event creation date even though it was returning the event start date:

function events_by_month($postType, $taxonomy, $taxArgs) {
  $months = array();

  $posts = get_posts(array(
    'posts_per_page' => -1,
    // 'orderby' => 'post_date',
    // 'order' => 'ASC',
    'post_type' => $postType,
    $taxonomy   => $taxArgs,
    'post_status' => 'publish',
    'suppress_filters'=>false
  ));

  foreach($posts as $post) {
    $event_obj = $post->EE_Event;
    $date_times = $event_obj->first_datetime();
    $event_start = strtotime($date_times->start_date());

    $months[date('F Y', strtotime( $date_times->start_date() ))][] = $post;

  }
  return $months;
}


Josh

  • Support Staff

August 30, 2016 at 3:38 pm

So in your first example, it’s not using the $wp_query global, which is fine, but your loop syntax is missing the $events_query variable.

Here are a few other example that should help you craft together what you need:

https://gist.github.com/joshfeck/6e33532c37a123bbf532

https://gist.github.com/joshfeck/e3c9540cd4ccc734e755


yamamDev

August 30, 2016 at 4:03 pm

Not sure how I didnt notice the missing variable there, thank you. So heres what my function looks like now and im still getting the “Fatal error on a non-object” error:

function events_by_month($taxonomySlug) {
  // array to use for results
  $months = array();
  // get posts from WP
  $eventAtts = array(
    'limit' => -1,
    'order_by' => 'start_date',
    'sort' => 'ASC',
    'category_slug' => $taxonomySlug,
    'post_status' => 'publish',
    'show_expired' => FALSE
  );
  // loop through posts, populating $months arrays
  global $events_query;
  $events_query = new EE_Event_List_Query( $eventAtts );
  if ( $events_query->have_posts()) : while ( $events_query->have_posts()) : $events_query->the_post();

    $event_obj = $post->EE_Event;
    $date_times = $event_obj->first_datetime();
    // $event_day = date('j', $event_start);
    // $event_month = date('F', $event_start);
    // $event_year = date('Y', $event_start);

    // $x = $post->EE_Event->first_datetime(); 

    $months[date('F Y', strtotime( $date_times->start_date() ))][] = $post;
  endwhile;
  endif;
  // reverse sort by year
  krsort($months);
  return $months;
}


Josh

  • Support Staff

August 30, 2016 at 4:20 pm

I don’t think you can get the event object from the post object like you tried there. Here’s another example you can step through as well:

https://gist.github.com/joshfeck/4e99a0e2d81d7c5641a2


yamamDev

August 30, 2016 at 8:37 pm

the second post in this thread shows a near working example of what im trying to accomplish, and I did get the event object using the same syntax. The only issue with that function was that it was returning an array that was sorted by the event(post) creation date instead of the event’s start date.

Ill look through the couple examples you’ve posted and see what i can put together, but if you can find a way to get the event object in the event query that would be ideal.

Thank you!


yamamDev

August 31, 2016 at 8:25 am

got the function working now! thanks for all the help!

function events_by_month() {
  // array to use for results
  $months = array();
  // set up show expired = false
  $date_format    = get_option( 'date_format' );
  $time_format    = get_option( 'time_format' );
  $args = array(
    'limit'            => -1,
    'sort'             => 'ASC', 
    'order_by'         => 'start_date',
    'category_slug'    => 'ecap-events',
    'show_expired'     => FALSE,
  );
  $loop = new EE_Event_List_Query( $args );
  if( $loop->have_posts() ):
    while ( $loop->have_posts() ) : $loop->the_post();
      $eventPost = get_post( get_the_id() );
      $datetimes = EEM_Datetime::instance()->get_datetimes_for_event_ordered_by_start_time( $eventPost->ID, false, false, 1 );
      foreach ( $datetimes as $datetime ):
        $months[date('F Y', strtotime( $datetime->start_date() ))][] = $eventPost;
      endforeach;
    endwhile;  
  endif;        
  return $months;
  wp_reset_postdata();
}

The support post ‘Using EE_Event_List_Query to sort events by start date instead of post date’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso