how to put newer events first?

This topic contains 7 replies, has 3 voices, and was last updated by  Stephen Harris 9 years, 11 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #8465

    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
    #8469

    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
    #8479

    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
    #8489

    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
    #8501

    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
    #8502

    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
    #15627

    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
    #15883

    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
Viewing 8 posts - 1 through 8 (of 8 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.