Support

Home Forums Event Espresso Premium Query Category & Order_by start_date

Query Category & Order_by start_date

Posted: March 16, 2016 at 4:48 pm

Viewing 3 reply threads


michaelstoneteaching

March 16, 2016 at 4:48 pm

This seems like it should be a relatively simple solution but it doesn’t seem straight forward in the docs. Would love if someone can help me out.

I am trying to create this query on the events.

` <?php
$args = array(
‘post_type’ => ‘espresso_events’,
‘posts_per_page’ => -1,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘espresso_event_categories’,
‘field’ => ‘slug’,
‘terms’ => ‘retreat’,
‘order_by’ => ‘start_date’
),
),
);
$the_query = new WP_Query( $args );
?>`


michaelstoneteaching

March 16, 2016 at 4:49 pm

 <?php
    $args = array(
      'post_type' => 'espresso_events',
      'posts_per_page' => -1,
      'tax_query' => array(
        array(
          'taxonomy' => 'espresso_event_categories',
          'field'    => 'slug',
          'terms'    => 'retreat',
          'order_by' => 'start_date'
        ),
      ),
    );
    $the_query = new WP_Query( $args );
  ?>

Hopefully that’s more legible.


michaelstoneteaching

March 16, 2016 at 5:02 pm

$atts = array(
        'title' => NULL,
        'limit' => 100,
        'css_class' => NULL,
        'show_expired' => FALSE,
        'month' => NULL,
        'category_slug' => 'retreat',
        'order_by' => 'start_date',
        'sort' => 'ASC'
      );
      
      $the_query = new EE_Event_List_Query( $atts );

That works great – but if I want to pass in multiple categories to the category-slug param.

Attempted 'category_slug' => 'workshop, intensive',
'category_slug' => array('workshop', 'intensive'),
'category_slug' => array('workshop, intensive')

Any thoughts would be amazing. Thanks so much.


Tony

  • Support Staff

March 17, 2016 at 3:13 pm

Hi there,

I think I may have misunderstood your initial question previously (via Email), I’ll explain little further and if so I do apologize.

So a quick explanation, EE_Event_List_Query is basically a wrapper for WP_Query which means that you can pass values in the same way you would to WP_Query.

‘category_slug’ is actually a parameter of EE_Event_List_Query and is designed to use a single category slug to display the events for that category so if use multiple categories there it won’t work.

To do this you’ll need to use the ‘tax_query’ parameter (which is what you asked about previously) within the arguments and pass over each category slug you need, like this:

'tax_query' => array(
	'relation' => 'OR',
	array(
		'taxonomy' => 'espresso_event_categories',
		'field' => 'slug',
		'terms' => array( 'category-slug-1' ),
	),
	array(
		'taxonomy' => 'espresso_event_categories',
		'field' => 'slug',
		'terms' => array( 'category-slug-2' ),
	)
),

So your $atts would look like:

$atts = array(
	'title' => NULL,
	'limit' => 100,
	'css_class' => NULL,
	'show_expired' => FALSE,
	'month' => NULL,
	'tax_query' => array(
		'relation' => 'OR',
			array(
				'taxonomy' => 'espresso_event_categories',
				'field' => 'slug',
				'terms' => array( 'workshop' ),
			),
			array(
				'taxonomy' => 'espresso_event_categories',
				'field' => 'slug',
				'terms' => array( 'intensive' ),
			)
	),
	'order_by' => 'start_date',
	'sort' => 'ASC'
);

You can also do this using the EE Models which at its most basic would look like this:

$events = EEM_Event::instance()->get_all(
	array(
		array(
			'Term_Taxonomy.Term.slug' => array( 'in', array(
				'workshop',
				'intensive',
			))
		)
	)
);

$events would contain only event object from those categories, however note that it will pull the events regardless of the events current status as it is above, so expire events will be included.

Does that help?

Viewing 3 reply threads

The support post ‘Query Category & Order_by start_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