Query modifications with tribe_events_pre_get_posts not honored via AJAX

Home Forums Calendar Products Events Calendar PRO Query modifications with tribe_events_pre_get_posts not honored via AJAX

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #992821
    substance
    Participant

    I’m trying to remove an Event Category (id:462) from all event queries (except that category’s landing page) and it works fine on page load, but doesn’t seem to be honored when query results are loaded via ajax.

    The following code works on events page load, and if I were to load an event search query directly on a page (i.e., reloading the results), but if I do anything that would reload the query via AJAX it is not honored (such as paginating, or entering a new search). When that happens, events of the category 462 still show up.

    I’m thinking that somewhere the date query is superseding this query modifications, but no matter what I set as a the priority of this modification, the AJAX calls are still not catching it. Any ideas?

    // Tribe Events modifications to the query
    function events_mod( $query ) {
      if( !is_admin() && $query->is_main_query() && !is_tax('tribe_events_cat') && !tribe_is_event() && ( tribe_is_past() || tribe_is_upcoming() ) ) {
        $taxquery = array(
            array(
              'taxonomy' => 'tribe_events_cat',
              'field'    => 'id',
              'terms'    => array( 462 ),
              'operator' => 'NOT IN'
            )
          );
        $query->set( 'tax_query', $taxquery );
      }
      return $query;
    }
    
    add_action( 'tribe_events_pre_get_posts', 'events_mod', 1);
    #992891
    substance
    Participant

    Ah I got it. Had a couple problems here:

    • I had to remove !is_admin() check since any ajax request validates that to true
    • I added defined( 'DOING_AJAX' ) && DOING_AJAX to catch the date-range query made via ajax.

    Now this works as expected, the entire logic call now is:
    if( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || $query->is_main_query() && !is_tax('tribe_events_cat') && !tribe_is_event() && ( tribe_is_past() || tribe_is_upcoming() ) )

    #993109
    Brian
    Member

    I am glad to see you were able to figure it out.

    I am going to go ahead and close this ticket. If you have a similar issue or another in the future, please do not hesitate to create a new ticket.

    Thanks!

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Query modifications with tribe_events_pre_get_posts not honored via AJAX’ is closed to new replies.