Filtering custom post admin listing page elements based on acf user field

Question

I have the following problem. I have a wordpress user role ‘uzletkoto’, which I created with the user role editor plugin and set its rights. I have a unique post type ‘partner’, which has a user field created with the advanced custom fileds plugin, which returns a user id. I want the users set in this field to be able to see the post from the ‘uzletkoto’ role on the admin listing page. They are currently viewing all posts.

Any idea?

My following code hides all the posts:

function restrict_partner_access_in_admin( $query ) {
    global $pagenow;
    

    // Ellenőrizze, hogy az aktuális felhasználó 'uzletkoto' szerepkörbe tartozik-e, és hogy az aktuális oldal az 'edit.php' oldal a WordPress admin felületén.
    if ( is_admin() && is_user_logged_in() && in_array( 'uzletkoto', wp_get_current_user()->roles ) && $pagenow == 'edit.php' && isset( $query->query['post_type'] ) && $query->query['post_type'] == 'partner' ) {
        // Lekérjük az aktuális bejegyzés azonosítóját.
        $post_id = get_queried_object_id();

        // Lekéri az advanced custom field user mező értékét.
        $user_id = get_field( 'partneri_uzletkoto', $post_id );
       

        // Módosítja a WP_Query objektum query() metódusának args tömbjét, hogy csak azokat a 'partner' típusú bejegyzéseket jelenítse meg, amelyek az advanced custom field user mezőjében be vannak állítva a felhasználó azonosítójával.
        $query->set( 'meta_query', array(
            array(
                'key'     => 'partneri_uzletkoto',
                'value'   => $user_id_string,
                'compare' => 'EXISTS',
            ),
        ));
}
 add_action( 'pre_get_posts', 'restrict_partner_access_in_admin' );   }

Thank you very much

0
c001os 3 weeks 2023-03-05T04:09:43-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse