• Resolved puresaian

    (@puresaian)


    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 !!

    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

    (@d0153)

    Hi @puresaian

    Thanks for reaching out.

    One of my colleagues suggested the following snippet:

    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 to avoid unnecessary downtime.

    Let me know how it goes.

    Thread Starter puresaian

    (@puresaian)

    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 :

     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

    (@d0153)

    Hi @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 puresaian

    (@puresaian)

    Hello,
    Do you have any solutions for this ?
    Thank you ??

    Plugin Support Abz

    (@abzlevelup)

    Hi @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

    'compare' => 'LIKE'

    since it is an ID, no need for a wildcard.

     '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/.

    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

    (@d0153)

    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

    (@d0153)

    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.