Support

Home Forums Event Espresso Premium Order custom taxonomy template by start date and do not show expired

Order custom taxonomy template by start date and do not show expired

Posted: September 16, 2016 at 2:22 pm

Viewing 3 reply threads


luis camargo

September 16, 2016 at 2:22 pm

Hello EE team.

I managed to create a custom taxonomy with the following code on functions.php, and created a simple taxonomy-ciudad.php on my template to show my posts.

The problem is, I have no idea how to filter the query for my taxonomy template, so it gets only active events, and order them by start date. Normally I’d use a function on my template’s functions.php with pre_get_post and order by meta_values, but EE4 creates the dates on its own db table, not on postmeta. So offcourse something like this won’t work:


//This code has no use because start_date is not a meta_key
add_action( 'pre_get_posts', 'customise_speakers_taxonomy_archive_display' );
	function customise_speakers_taxonomy_archive_display ( $query ) {
		if (($query->is_main_query()) && (is_tax('ciudad'))){
			$query->set( 'posts_per_page', '9' );
			$query->set( 'meta_key', 'start_date' );           
        		$query->set( 'orderby', 'meta_value' );
			$query->set( 'order', 'DESC' );
		}
	}

Even though I really want to use the taxonomy-ciudad.php template, I also tried to use a custom query like the one here: https://gist.github.com/idliketofly/d754f45c0861023d7529 but I didn’t know how to make it work with the custom taxonomy.

I currently use Event Espresso 4.9.11.p on WordPress 4.6.1.

I’d appreciate any help.

  • This topic was modified 8 years, 3 months ago by Josh. Reason: move larger block of code to a gist for better readability
  • This topic was modified 8 years, 3 months ago by luis camargo.


Josh

  • Support Staff

September 16, 2016 at 2:37 pm

Just because event date times aren’t stored as meta values, that doesn’t mean you can’t order by those dates, or filter out expired events.

For example you can filter out expired events from your query with:

function custom_posts_where_sql_for_only_expired() {
	return ' AND ' . EEM_Datetime::instance()->table() . '.DTT_EVT_end < "' . current_time( 'mysql', TRUE ) . '" ';
}

then before the loop:

add_filter( 'posts_where', 'custom_posts_where_sql_for_only_expired' );

// then after the loop, remove that filter
remove_filter( 'posts_where', 'custom_posts_where_sql_for_only_expired' );

There are a few other examples in the WordPress codex:

https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_orderby


luis camargo

September 19, 2016 at 8:09 am

Thanks for your help. I think I’m starting to get somewhere but I still can’t get it to work. I tried putting the filters on taxonomy-ciudad.php, before the loop and removing them after, but as you can see here they work only on functions.php and didn’t do anything at all.

I created something like this but when I use any of the 3 filters the query returns no results. This code is on functions.php on my template. Do you know if I’m missing anything? I tried only the first one with your ‘EEM_Datetime::instance()->table()’ but it didn’t work either.

  • This reply was modified 8 years, 3 months ago by Josh. Reason: move larger block of code to a gist for better readability


Josh

  • Support Staff

September 23, 2016 at 12:14 pm

Yeah that’s not going to work, here’s something that you can try instead:

https://gist.github.com/joshfeck/26e0e27bb9c9fa7ed7159c0c6c9704ea

Viewing 3 reply threads

The support post ‘Order custom taxonomy template by start date and do not show expired’ 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