Ordering events by title — query not working

Home Forums Calendar Products Events Calendar PRO Ordering events by title — query not working

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1006049
    Figoli Quinn
    Participant

    I have a situation where I’m pulling out a bunch of recurring events to show them as a list of “clubs.” Right now, I’m just querying them by category. So if you set up a recurring event for the “book club”, say, you can click the category to have it show up on the “clubs” page. All this is working, except that I want to order them alphabetically by title. That portion of the custom query is not having the expected result. I see some effect if I change “ASC” to “DESC”, but the orderby piece does not seem to be working. Ideas?

    <?php
    
    	$oasis_groups = tribe_get_events(
    		array(
    			'post_type' => 'tribe_events',
    			'tribe_events_cat' => 'show-on-groups-page',
    			'posts_per_page' => -1,
    			'tribeHideRecurrence' => 1,
    			'orderby' => 'title',
    			'order' => 'ASC',
    		)
    	);
    
    	foreach( $oasis_groups as $group ) {
    		setup_postdata( $group );
    ?>
    // view stuff ...
    #1006272
    Josh
    Participant

    Hey Robert,

    Thanks for following up with us!

    You can accomplish this by taking advantage of the “pre_get_posts()” hook on the calendar loops that you would like altered. The following snippet should help you in setting up the order you would like:

    
    add_filter( 'pre_get_posts', 'tribe_change_event_order', 99 );
    function tribe_change_event_order( $query ) {
    if ( tribe_is_past() || tribe_is_upcoming() || tribe_is_photo() ) {
    $query->set( 'orderby', 'title' );
    $query->set( 'order', 'ASC' );
    }
    return $query;
    }
    

    The key here is to ensure you hook it in pretty late (99 here) so it fires after the default sorting is set.

    Let me know if this helps.

    Thanks!

    #1008513
    Figoli Quinn
    Participant

    Thanks, Josh. This works for normal lists — that is it operates on the main query. I was running a secondary loop for some events, so this doesn’t quite do what I need. I’m going to keep looking into this.

    #1008869
    Geoff
    Member

    Hey Robert–just following up with you while Josh is out on vacation.

    Thanks for keeping us posted here and let us know if you come across anything that would help out. And, of course, we’re here to help with any further questions you might have on this. 🙂

    Cheers,
    Geoff

    #1008998
    Figoli Quinn
    Participant

    Well, man. I spent a good deal of time with this and just could not figure out how prevent ‘tribe_get_events’ from stomping on any ‘orderby’ parameter I tried to pass in — overriding it with ‘event_start_date’. Hooking into ‘pre_get_posts’ works, but that effects all queries. I just wanted one custom loop that did something a little different.

    I also tried to solve this problem just using a straight WP_Query on the ‘tribe_events’ post type. But I couldn’t figure out how not to get all recurrences of an event. (I just wanted the master event.)

    So I punted and decided the solution was to just manipulate the array of posts after the ‘tribe_get_events()’ query comes back.

    So I use ‘tribe_get_events’ for a custom query, grabbing all items from a specific category — but just the master event, not recurrences — ‘tribeHideRecurrence => 1’ seems to accomplish the latter.

    This gives you an array of post objects, on which you can then use the PHP functions ‘usort()’ and ‘strcmp()’ to shuffle them into alpha order by the post_title property. Then we move on to the loop to echo stuff out.

    Not as elegant as just getting the right query, but it seems to be working.

    <?php
    
    	$args = array(
    		'post_type' => 'tribe_events', // this is the default with tribe_get_events so actually not necessary if that's where you are passing these args
    		'tribe_events_cat' => 'my-special-event-category', //grabbing posts from the events category I want
    		'posts_per_page' => -1, // gets all such posts
    		'tribeHideRecurrence' => 1, // prevents showing all copies of recurring events, just gets the master. I don't actually know why this works, but it seems to.
    	);
    	
    	$custom_events = tribe_get_events( $args );
    	$custom_events = my_events_sort( $custom_events );
    
    	function my_events_sort( $my_array ) {    // this function can go in your functions.php file instead of here in the template file
    		
    		function my_title_sort( $a, $b )	{
    		    return strcmp( $a->post_title, $b->post_title );
    		}
    
    		usort( $my_array, 'my_title_sort' );
    		
    		return $my_array;
    	}
    	
    	foreach( $custom_events as $event ) {
    		setup_postdata( $event );
    ?>
    // echo out stuff ...
    
    • This reply was modified 10 years, 6 months ago by Figoli Quinn.
    • This reply was modified 10 years, 6 months ago by Figoli Quinn.
    #1009161
    Josh
    Participant

    Hey Robert,

    This is definitely an interesting approach! Glad you were able to get it working the way you wanted.

    I’ll go ahead and close this thread. If you have any further questions, please don’t hesitate to open a new thread.

    Thanks!

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Ordering events by title — query not working’ is closed to new replies.