Custom WP Query

Home Forums Calendar Products Events Calendar PRO Custom WP Query

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1121512
    James
    Participant

    Hi,

    I’m working on integrating Events Calendar with Geo My Wp.

    I have an issue with both plugins doing their own filtering on the query, which results in the ‘as EventStartDate’ which I believe is added by Events Calendar not being added, resulting in the query crashing when it later tries to order by ‘EventStartDate’.

    I don’t need my query to order by anything (it’s just putting pins on a map), so i’m looking for a way to override this ordering that Events Calendar adds. It seems like it adds it automatically via a filter? I could use ‘suppress filter’, but that will also suppress the Geo My Wp filter that I do need to run.

    So is there a way I can just stop the event order filter being added?

    Thanks,
    James.

    #1121827
    Nico
    Member

    Hi James,

    Thanks for reaching out to us! Let’s see what’s going on here…

    I have an issue with both plugins doing their own filtering on the query, which results in the ‘as EventStartDate’ which I believe is added by Events Calendar not being added, resulting in the query crashing when it later tries to order by ‘EventStartDate’.

    Can you please let me know which query are you referring to? If you can share the query code ti would be great!

    Thanks,
    Nico

    #1122052
    James
    Participant

    It’s a bit complicated. I’m using a global map feature in geo my wp which prepares the query automatically on a post type, the problem being that it shows all events not just future events.

    So i’m using a hook to add query arguments to the query:

    function gmw_filer_global_maps( $gmw ) {
    
        $gmw['query_args'] = array(
            //'suppress_filters' => true,
              'post_type' => 'tribe_events',
              'posts_per_page' => '12',
              'orderby' => 'none',
              //'post_status'    => 'publish',
              // 'meta_query'     => array(
              //   array(
              //     'key'     => 'event_date',
              //     'value'   => array( date( 'Ymd', strtotime( '-1 day' ) ), date( 'Ymd', strtotime( '+31 days' ) ) ),
              //     'compare' => 'BETWEEN',
              //     'type'    => 'DATE'
              //   ) 
              //)
            );
    
        var_dump($gmw['query_args']);
        return $gmw;
    }
    add_filter( 'gmaps_gmpt_form_before_posts_query', 'gmw_filer_global_maps' );

    So the issue is that that it produces this query:

    SELECT
    				SQL_CALC_FOUND_ROWS *
    			FROM (
    				SELECT  DISTINCT wp_posts.ID, wp_posts.post_type, gmwlocations.lat, gmwlocations.long,
    			gmwlocations.address, gmwlocations.formatted_address, gmwlocations.map_icon,
    			gmwlocations.phone, gmwlocations.fax, gmwlocations.email, gmwlocations.website  FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) LEFT JOIN wp_postmeta as tribe_event_end_date ON ( wp_posts.ID = tribe_event_end_date.post_id AND tribe_event_end_date.meta_key = '_EventEndDate' )  INNER JOIN wp_places_locator gmwlocations ON ID = gmwlocations.post_id  WHERE 1=1  AND ( 
      wp_postmeta.meta_key = '_EventStartDate'
    ) AND wp_posts.post_type = 'tribe_events' AND (wp_posts.post_status = 'publish') AND (wp_postmeta.meta_value >= '2016-06-03 07:07:45' OR (wp_postmeta.meta_value <= '2016-06-03 07:07:45' AND tribe_event_end_date.meta_value >= '2016-06-03 07:07:45' )) AND ( gmwlocations.lat != '0.000000' AND gmwlocations.long != '0.000000' )  ORDER BY EventStartDate ASC 
    			) a
    			GROUP BY IF( post_parent = 0, ID, post_parent )
    			ORDER BY EventStartDate ASC
    			LIMIT 0, 12

    So it hasn’t got the ‘as EventStartDate’ defined at the start of the query, so it breaks when it tries to order by ‘EventStarteDate’ at the end. As I said, I don’t need it to order by EventStartDate, and so if I can just stop the events calendar adding this bit to query it will work fine.

    Is there any way to do that?

    Thanks,
    James

    #1122078
    James
    Participant

    I’ve found a work around, but involves hacking the events plugin so I’m hoping you can help me with a more elegant solution.

    I’ve commented out line 658 in the-events-calendar/src/Tribe/Query.php
    $order_sql = "EventStartDate {$order}";

    Which has stopped events calendar adding an order by to any WP Query. This has fixed the conflict with the GMW plugin.

    I’ve then added the following to my other queries that need to be ordered by Event date:

    'orderby' => '_EventStartDate',
    'order' => 'ASC',

    Obviously this will break in any future plugin updates though.

    #1122165
    Nico
    Member

    Thanks for following up!

    Editing the core files is never a good idea. Off the top of my head I guess setting a higher priority in your filter might work. Something like:


    add_filter( 'gmaps_gmpt_form_before_posts_query', 'gmw_filer_global_maps', 100 );

    If this doesn’t work as expected you can use the filter tribe_events_query_posts_orderby to change the SQL that will be added to define order -line 702 the-events-calendar/src/Tribe/Query.php.

    Please let me know about it,
    Best,
    Nico

    #1128878
    Support Droid
    Keymaster

    This topic has not been active for quite some time and will now be closed.

    If you still need assistance please simply open a new topic (linking to this one if necessary)
    and one of the team will be only too happy to help.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Custom WP Query’ is closed to new replies.