Title: Alter Query &amp; pre_get_posts
Last modified: November 14, 2023

---

# Alter Query & pre_get_posts

 *  Resolved [Damien](https://wordpress.org/support/users/puresaian/)
 * (@puresaian)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/alter-query-pre_get_posts/)
 * hi,
 * I need to **only** display events to users who have the same ACF field in common
   with the event (not the others, never)
 * I use this code below which works very well but takes **extremely long** to process.
 * Is there a way to filter my events more efficiently? There are more than 2000
   events and it can sometimes take more than 10 seconds to display the calendar.
 * Thank you guys !!
 *     ```wp-block-code
       add_action( 'pre_get_posts', 'get_tribe_events_posts_exclude_cancelled' );
       function get_tribe_events_posts_exclude_cancelled( $query ) {
   
       if ( ( in_array ( $query->get('post_type'), array('tribe_events') ) && ! is_admin() && ! is_front_page() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX && ! empty( $query->query['tribe_render_context'] ) && 'default' === $query->query['tribe_render_context'] && ! empty( $query->query['post_type'] ) && Tribe__Events__Main::POSTTYPE === $query->query['post_type'] )) {
           $bp = buddypress();
           $user_id = $bp->loggedin_user->id;
           $user_caisse = get_field('caisse','user_'. $user_id);
   
           $metaquery[] = array(
   
           array (
               'relation'      => 'AND',
               array(
                   'eventDisplay'   => 'custom',
                   'start_date' => 'now',
               ),
               array(
                   'relation' => 'OR',
                   /* Event Natio */
                   array(
                       'key'         => '_caisse_key',
                       'value' => '',
                   ),
                   /* Ou Event Caisse */
                   array(
                       'relation' => 'AND',
                       array(
                           'key'         => '_caisse_key',
                           'value'   => $user_caisse[0]->ID,
                           'compare' => 'LIKE'
                       ),
   
                   ),
               ),
   
           )
       );
       $query->set('meta_query', $metaquery);
       }
       }
       ```
   

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Support [Darian](https://wordpress.org/support/users/d0153/)
 * (@d0153)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/alter-query-pre_get_posts/#post-17200813)
 * Hi [@puresaian](https://wordpress.org/support/users/puresaian/)
 * Thanks for reaching out.
 * One of my colleagues suggested the following snippet:
 *     ```wp-block-code
       add_action('pre_get_posts', 'get_tribe_events_posts_exclude_cancelled');
   
       function get_tribe_events_posts_exclude_cancelled($query) {
           if (!is_admin() && !is_front_page() && (defined('DOING_AJAX') && DOING_AJAX && !empty($query->query['tribe_render_context']) && 'default' === $query->query['tribe_render_context'] && !empty($query->query['post_type']) && Tribe__Events__Main::POSTTYPE === $query->query['post_type'])) {
               $bp = buddypress();
               $user_id = $bp->loggedin_user->id;
               $user_caisse = get_field('caisse', 'user_' . $user_id);
   
               $metaquery = array(
                   'relation' => 'AND',
                   array(
                       'eventDisplay' => 'custom',
                       'start_date'   => 'now',
                   ),
                   array(
                       'relation' => 'OR',
                       // Event Natio
                       array(
                           'key'   => '_caisse_key',
                           'value' => '',
                       ),
                       // Event Caisse
                       array(
                           'relation' => 'AND',
                           array(
                               'key'     => '_caisse_key',
                               'value'   => $user_caisse[0]->ID,
                               'compare' => 'LIKE',
                           ),
                       ),
                   ),
               );
   
               $query->set('meta_query', array($metaquery));
           }
       }
       ```
   
 * As always, please test this first on a [staging version of your live site](https://theeventscalendar.com/knowledgebase/creating-and-using-a-wordpress-staging-site/)
   to avoid unnecessary downtime.
 * Let me know how it goes.
 *  Thread Starter [Damien](https://wordpress.org/support/users/puresaian/)
 * (@puresaian)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/alter-query-pre_get_posts/#post-17201158)
 * Hi Darian,
 * Thanx for your response.
 * Your code does not work, the request is no longer altered.
   A “var_dump($query)”
   no longer displays anything.
 * Are you sure about these :
 *     ```wp-block-code
        if (!is_admin() && !is_front_page() && (defined('DOING_AJAX') && DOING_AJAX && !empty($query->query['tribe_render_context']) && 'default' === $query->query['tribe_render_context'] && !empty($query->query['post_type']) && Tribe__Events__Main::POSTTYPE === $query->query['post_type'])) {}
       ```
   
 * Thanx again 🙂
 *  Plugin Support [Darian](https://wordpress.org/support/users/d0153/)
 * (@d0153)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/alter-query-pre_get_posts/#post-17210582)
 * Hi [@puresaian](https://wordpress.org/support/users/puresaian/)
 * Thanks for your response.
 * Let me check with the team about this, and I’ll get back to you once I know more.
 * In the meantime, let me know if you have other questions or concerns.
 *  Thread Starter [Damien](https://wordpress.org/support/users/puresaian/)
 * (@puresaian)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/alter-query-pre_get_posts/#post-17250580)
 * Hello,
   Do you have any solutions for this ?Thank you 🙂
 *  Plugin Support [WilloftheD](https://wordpress.org/support/users/abzlevelup/)
 * (@abzlevelup)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/alter-query-pre_get_posts/#post-17253566)
 * Hi [@puresaian](https://wordpress.org/support/users/puresaian/), thanks for your
   patience here. While our ability to help with **customizations and 3rd-party 
   integrations is limited**, I’d like to learn more about this to try to help!
 * That being said, I do think that your initial conditions here are correct, although
   that would be a lot of data in one query. One thing I could suggest is to add
   an `end_date` that could limit and lessen the query time.
 * Also, instead of
 *     ```wp-block-code
       'compare' => 'LIKE'
       ```
   
 * since it is an `ID`, no need for a wildcard.
 *     ```wp-block-code
        'compare' => '=' 
       ```
   
 * Another way would be via **ORM**, you’d might need a dev for this one if you’re
   not familiar — you can learn more about it here → [https://theeventscalendar.com/knowledgebase/show-events-by-custom-field/](https://theeventscalendar.com/knowledgebase/show-events-by-custom-field/).
 * Last thing is to upgrade server ram/memory to execute the query a bit faster 
   if you have the resources.
 * Hope that helps. Have a great day.
 *  Plugin Support [Darian](https://wordpress.org/support/users/d0153/)
 * (@d0153)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/alter-query-pre_get_posts/#post-17263260)
 * Hi there,
 * I hope you’re doing well. I just wanted to touch base and check in with you. 
   It’s been a little while since we’ve heard from you. I was just curious if you
   had the chance to try out the recommendation provided above.
 * Let me know if there’s anything I can assist you with.
 *  Plugin Support [Darian](https://wordpress.org/support/users/d0153/)
 * (@d0153)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/alter-query-pre_get_posts/#post-17271295)
 * Hi there,
 * This thread has been inactive for a while, so we’ll go ahead and mark it **Resolved**.
   Please open a new thread if any other questions arise, and we’d be happy to help.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Alter Query & pre_get_posts’ is closed to new replies.

 * ![](https://ps.w.org/the-events-calendar/assets/icon-256x256.gif?rev=2516440)
 * [The Events Calendar](https://wordpress.org/plugins/the-events-calendar/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/the-events-calendar/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/the-events-calendar/)
 * [Active Topics](https://wordpress.org/support/plugin/the-events-calendar/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/the-events-calendar/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/the-events-calendar/reviews/)

## Tags

 * [ajax](https://wordpress.org/support/topic-tag/ajax/)
 * [filter](https://wordpress.org/support/topic-tag/filter/)

 * 7 replies
 * 3 participants
 * Last reply from: [Darian](https://wordpress.org/support/users/d0153/)
 * Last activity: [2 years, 6 months ago](https://wordpress.org/support/topic/alter-query-pre_get_posts/#post-17271295)
 * Status: resolved