Hi,
Events titles appear from older ones to newer ones when I search them from ‘archives’.
Is there any way I can make the search results ‘Newer to Older’?
Thank you.

Anh Lio
Hi Anh,
add_action('pre_get_posts','anh_alter_query',15);
function anh_alter_query($query){
if( $query->is_main_query() && is_post_type_archive('event') ){
$query->set('orderby','eventstart');
$query->set('order','desc');
}
}
should do it.
Keep mind that this means an event next year appears before an event next week. If you just want the next event to appear first then you probably want to keep the current (chronological) sort order and instead hide past events – either via the plug-in options or:
add_action('pre_get_posts','anh_hide_past_events',15);
function anh_hide_past_events($query){
if( $query->is_main_query() && is_post_type_archive('event') ){
$query->set( 'showpastevents', false );
}
}

Stephen Harris
Hi,
Thank you for your help. Still one question: where should I put the code into?
I know little about coding, please specify the file name and context for me. Thanks a lot…

Anh Lio
HI Anh,
That code can go in a utility plug-in or your theme’s functions.php
(but the former is recommended – see this post: http://wp-event-organiser.com/blog/tutorial/where-should-i-put-code-from-the-tutorials/)

Stephen Harris
Thank you.
It works.
And I still need your help, because I want this function in ‘taxonomy catogary’ and ‘archive’ pages.
But this ‘site-utility’ plugin doesn’t seem to work on those pages. Could you please
teach me how to do it?
Thanks a million.

Anh Lio
Your welcome :). To include event category pages:
add_action('pre_get_posts','anh_hide_past_events',15);
function anh_hide_past_events($query){
if( $query->is_main_query() && ( is_tax( 'event-category') || is_post_type_archive('event') ) ){
$query->set( 'showpastevents', false );
}
}
(see this post for more details: http://wp-event-organiser.com/forums/topic/custom-category-archive-pages/#post-8428)

Stephen Harris
Hi! I’m looking to get this working on the category archive page of a new install. I’ve tried the code above (all versions of it) and its not working. Is there a way to do this now with the most recent version of Pro?
Thanks!
-
This reply was modified 10 years ago by
Aaron Robb.

Aaron Robb
The priority should probably be lowered to below 10:
add_action('pre_get_posts','anh_hide_past_events', 9 );
function anh_hide_past_events($query){
if( $query->is_main_query() && ( is_tax( 'event-category') || is_post_type_archive('event') ) ){
$query->set( 'showpastevents', false );
}
}

Stephen Harris