I have a custom post type called Projects that includes an ACF field named team_members which uses User as a field type and returns a User Array. I want to filter the post type listing so that the current logged-in user sees only records that contain their data in the team_members field.

I am trying to use this query, but it returns all the records:

add_action('pre_get_posts', 'filter_posts_list');
function filter_posts_list($query)
{
    //$pagenow holds the name of the current page being viewed
    global $pagenow, $typenow;  

    //$current_user uses the global
    global $current_user;

    //Shouldn't happen for the admin, but for any role with the edit_posts capability and only on the posts list page, that is edit.php
    if(!current_user_can('administrator') && current_user_can('edit_posts') && ('edit.php' == $pagenow) &&  $typenow == 'projects') { 

        $current_user_id = $current_user->ID;
        
        $query->set( 'meta_query', array(
            array(
                'key'     => 'team_members',
                'value'   => $current_user_id,
                'compare' => "LIKE"
            ),
        ));
    
    }
}

Here is my custom field team_members:

custom field

And here is the example of a post from custom post type Projects:

the post from Projects

How can I query only by the ACF field team_members that has the exact user ID as the current user in the post?

0

There are 0 best solutions below