Home › Forums › Calendar Products › Events Calendar PRO › Ordering events by title — query not working
- This topic has 5 replies, 3 voices, and was last updated 10 years, 6 months ago by
Josh.
-
AuthorPosts
-
September 17, 2015 at 9:28 am #1006049
Figoli Quinn
ParticipantI 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 ...September 17, 2015 at 4:45 pm #1006272Josh
ParticipantHey 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!
September 24, 2015 at 2:11 pm #1008513Figoli Quinn
ParticipantThanks, 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.
September 25, 2015 at 3:17 pm #1008869Geoff
MemberHey 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,
GeoffSeptember 26, 2015 at 10:41 pm #1008998Figoli Quinn
ParticipantWell, 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.
September 28, 2015 at 7:34 am #1009161Josh
ParticipantHey 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!
-
This reply was modified 10 years, 6 months ago by
-
AuthorPosts
- The topic ‘Ordering events by title — query not working’ is closed to new replies.
