Support

Home Forums Event Espresso Premium Orderby in cusotm Query

Orderby in cusotm Query

Posted: February 18, 2015 at 9:48 am


Peyton Earey

February 18, 2015 at 9:48 am

Just need to display events by their Event start date.

Is my orderby parameter wrong?

// Load Event View Helper
EE_Registry::instance()->load_helper('Event_View');

    $events = get_posts(array(
		'post_type'=>'espresso_events',
		'suppress_filters'=>false,
		'posts_per_page' => 4,
		'orderby' => 'DTT_EVT_start',
		'order' => 'DESC'
	));


Josh

  • Support Staff

February 18, 2015 at 10:16 am

Hi Peyton,

The get_posts() function’s orderby parameter accepts the values listed here:

http://codex.wordpress.org/Template_Tags/get_posts#Parameters


Peyton Earey

February 18, 2015 at 10:38 am

Sorry, almost there. You guys have been a big help. I’m just trying to wade through your examples in your different queries.

This one doesn’t sort by start date but displays the start date exactly like I want just need it to sort correctly.

<?php
// Load Event View Helper
EE_Registry::instance()->load_helper('Event_View');

    $events = get_posts(array(
		'post_type'=>'espresso_events',
		'suppress_filters'=>false,
		'posts_per_page' => 4,
		'order_by' => 'start_date',
		'sort' => 'ASC'
	));
	foreach($events as $event) {
		
		$event_obj = $event->EE_Event;
		$link = $event_obj->get_permalink();
		$datetime = EEH_Event_View::get_primary_date_obj( $event->ID );
		$the_date = $datetime instanceof EE_Datetime ? $datetime->start_date( 'm/j' ) :  '';

?>
<div class="homelist">
    <a href="<?php echo $link; ?>">
        <div class="eventlistdate"><?php echo $the_date ?></div>
        <h3 class="event_title"><?php echo $event_obj->name() ?></h3>
    </a>
    </div>
<?php } ?>

This Query (taken from your example) sorts by start date, but it displays the “on sale date”, instead of the start date like I want.

<?php	
	
$atts = array(
	'title' => NULL,
	'limit' => 4,
	'css_class' => NULL,
	'show_expired' => TRUE,
	'month' => NULL,
	'category_slug' => NULL,
	'order_by' => 'start_date',
	'sort' => 'ASC'
);
// run the query
global $wp_query;
$wp_query = new EE_Event_List_Query( $atts );
	if (have_posts()) : while (have_posts()) : the_post(); 
	
		$event_obj = $post->EE_Event;
		$link = $event_obj->get_permalink();
		$datetime = EEH_Event_View::get_primary_date_obj( $post->ID );
		$the_date = $datetime instanceof EE_Datetime ? $datetime->start_date( 'm/j' ) :  '';
	
	?>  
    <div class="homelist">
    <a href="<?php echo $link; ?>">
        <div class="eventlistdate"><?php echo $the_date ?></div>
        <h3 class="event_title"><?php echo $event_obj->name() ?></h3>
        
    </a>
    </div>    
<?php endwhile; endif; ?> 

Just looking to sort by start date, and display the start date in any kind of query.

Thanks so much


Josh

  • Support Staff

February 18, 2015 at 12:02 pm

Hi Peyton,

So in your first example, please note that the orderby parameter for get_posts will not accept start_date.

In your second example, I tried to load your code up into a page template and it threw a fatal error. Then I changed where you have:

global $wp_query;
to
global $wp_query, $post;

and that allowed the event list to display. Along with that, I can verify that $the_date = $datetime instanceof EE_Datetime ? $datetime->start_date( 'm/j' ) : ''; displays the event’s start date. It does not display the goes on sale date.


Peyton Earey

February 18, 2015 at 12:38 pm

Ah, ok. Great. But what if I have Multiple date times for the event?

Here you can see what I have:
Multiple date times

I’ve changed the show expired parameter to FALSE, and I guess because the event is truly not expired, because it has later dates, it still shows the first date, which is expired and is a little confusing.

Expired date time

Thanks in advance.


Josh

  • Support Staff

February 19, 2015 at 5:09 pm

Hi there,

It’s actually because the you’re using the get_primary_date_obj() method. That’s always going to the display the primary, or first added datetime for an event. If you look at the code I shared with you in your other thread, you’ll see an example of how to get the next upcoming datetime for an event:

https://eventespresso.com/topic/custom-queries/#post-140961


Peyton Earey

February 20, 2015 at 6:44 am

And that makes sense. Thank you.

The support post ‘Orderby in cusotm Query’ 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