• After wasting pretty much 2 hours trying to fix this and looking at other posts I think I may have found the problem to a bug with The Events Calendar plugin version Version: 3.0.3

    The problem: when using the function tribe_get_events to retrieve events it seems to ignore the values of orderby and order resulting in posts always being retrieved by date in ascending order (earliest first) in my experience.

    I looked at this post carefully: http://wordpress.org/support/topic/plugin-the-events-calendar-events-list-in-chronological-order?replies=51

    and tried every solution to no avail.

    Then I noticed something odd in the function named: posts_orderby in the file:

    /lib/tribe-event-query.class.php in The Events Calendar plugin around line 482 (or something close to that).

    The function has two parameters $order_sql and $query and, in within the function code two variables: $order and $orderby, which, check for properties of $query like so:

    if ( $query->tribe_is_event || $query->tribe_is_event_category ) {
        $order = !empty( $query->order ) ? $query->order : $query->get( 'order' );
        $orderby = !empty( $query->orderby ) ? $query->orderby : $query->get( 'orderby' );

    So then I decided to do a var_dump of $query->order and $query->orderby and like I expected it return empty (or NULL) for both of them.

    So then I decided to do a var_dump of $query to see if the values I passed for orderby and order through my arguments array to the tribe_get_events function would appear anywhere and they did, but, not the way the code was checking for them. Rather, they appeared as within an array contained in property named query of the $query variable like so:

    $query->query['order'] and $query->query['orderby'] and this returned the values I had actually passed.

    So then I changed the above snippet of code to this:

    if ( $query->tribe_is_event || $query->tribe_is_event_category ) {
        $order = !empty( $query->query['order'] ) ? $query->query['order'] : $query->get( 'order' );
    
        $orderby = !empty( $query->query['orderby'] ) ? $query->query['orderby'] : $query->get( 'orderby' );

    and that seems to fix things pretty well.

    So what I wonder then, is if that is an official fix to the bug or if I’ve offset something or created another bug with my fix. Anyways, I haven’t come across this same fix anywhere else yet so I’m hoping this is accepted by Modern Tribe.

    Let me know what you think.

    Regards,

    racl101.

    http://wordpress.org/plugins/the-events-calendar/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Hi racl101,

    If it’s a bug we’re definitely interested in reviewing and fixing it, however can you provide a coded example of what you are doing with tribe_get_events() so we can check that everything is correct at that level first of all?

    Thanks!

    Thread Starter racl101

    (@racl101)

    Hi Barry,

    sorry for taking long to respond. Yes I can provide a code example of what I was trying to do and here it is:

    Basically, I was trying to pull Event Calendar posts into a custom feed to display not just upcoming events but all events (upcoming and passed). The part that wasn’t working the orderby and order arguments. It was always returning posts in ascending order by date, when I wanted most recently published events first.

    No matter what values I fed into orderby and and order the query returned the posts in the same order and that’s when I went about to looking for the solution that I found and posted .

    <?php
    
    		// valid eventDisplay options: ‘past’, ‘upcoming’, ‘day’, ‘all’, ‘month’
    		$args = array(
    			'eventDisplay'	=> 'all',
    			'orderby'	=> 'date',
    			'order'	=> 'DESC',
    			'posts_per_page' => 4,	// -1 to show all posts
    			'offset' => 1,
    			'tax_query'	=> array(
    				'relation'	=> 'AND',
    				array(
    					'taxonomy'	=> 'tribe_events_cat',
    					'field'			=> 'slug',
    					'terms'			=> $category['slug']
    				)
    			)
    		);
    
    		$posts_query = tribe_get_events($args);
    
    		if(!empty($posts_query)):
    
                      // ...

    Let me know what you think.

    Regards,

    OK, thanks!

    We’ll definitely get that logged, confirm a few things and if appropriate roll it into a maintenance release 🙂

    Spent hours looking for a solution to this ordering problem, reading years of related posts, and trying all suggestions. Then…

    racl101’s post encouraged me to try something.

    So on tribe-event-query.class.php on line 548 I changed
    ‘function set_order( $default = ‘ASC’ )’ to
    ‘function set_order( $default = ‘DESC’ )’
    and voilá! my problem was fixed.

    I’m a real amateur at coding, so is there a problem that anyone knows of with this fix——aside from having to re-fix it at the next update?

    Thanks, racl101!

    I’m also looking for a solution to this.

    jimkend: that solution will work until the next update as that class file will most likely be replaced by the new one that comes in the update, which is why it’s usually not recommended to change the core files to a plugin.

    I tried extending the TribeEventsQuery class and reset the set_orderby() function with no success, I’m guessing because it’s not a public function, but I could be wrong.

    I have a client who posts all their events as all day, and would like them arranged by title. I’ll keep working on the issue and update if I make any progress, as the “order” and “orderby” are similar functions.

    Thanks for the note of “solidarity,” and I look forward to hearing any progress you make with this. I have to live with my sloppy fix for the time being, as I have many other things to attend to on the site in question.

    Alright, I got my version of it to work the way I needed to, using pre_get_posts in the functions.php:

    function event_title_sort($query) {
    	if ( !is_admin() && ( $query->tribe_is_event || $query->tribe_is_event_category ) ) {
    		$query->set( 'orderby', 'title' );
    	}
    }
    add_action( 'pre_get_posts', 'event_title_sort', 100, 1 );

    jimkend: You could modify what I did, but be warned that this modifies it for any pulling of events post type.

    function reverse_event_sort($query) {
    	if ( !is_admin() && ( $query->tribe_is_event || $query->tribe_is_event_category ) ) {
    		$query->set( 'order', 'DESC' );
    	}
    }
    add_action( 'pre_get_posts', 'reverse_event_sort', 100, 1 );

    This would always set your query to ask for order descending (except for when you’re in the admin, hence the !is_admin() )

    Hope this helps!!

    Edit: I didn’t test this, but my version of it works, so, unless there’s a typo in mine, it should work for you too.

    Sounds like you’re making progress here – great to see solutions being shared.

    Plugin Contributor leahkoerper

    (@leahkoerper)

    Hey there,

    I just wanted to let you know that a fix for this issue is included in our upcoming version 3.3. Keep an eye out on your WordPress Plugins page for an update. Thanks for your patience while we worked on this!

    Cheers,
    Leah

    Thread Starter racl101

    (@racl101)

    Excelsior!

    And especial thanks to you, racl101, for raising and reporting this issue 🙂

    Plugin Contributor leahkoerper

    (@leahkoerper)

    Hi there,

    I’m happy to report that we have fixed this in our upcoming version 3.4. Keep an eye on your Plugins page for an update message! Thanks for your patience while we worked on this.

    Best,
    Leah

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Possible fix for tribe_get_events ignores values of 'order' and 'orderby' args!’ is closed to new replies.