I'd like to disable access to posts in the back-end/Admin from a user that doesn't have the privilege to view those posts. This will function similar to the Author role however this "privilege" is controlled by an ACF User relationship field and not by a WP Core role or privilege.
One level of complexity deeper, the User relationship field is actually pointing to a parent/grouping custom post type that is then tied to the children post elements I'm attempting to filter. So rather than show just the posts that user has authored, it's also showing posts that others have authored but that they have been granted permission to manage. Let me explain this below...
Custom Post Type: Shoes [shoe
]
Custom Post Type: Brands [brand
]
- Let's say you have a "shoe" called "Air Jordans" and you have a "brand" called "Nike".
- So you create an ACF bidirectional relationship between "Air Jordan" and "Nike".
- Now let's say you create a user on your site that you want to be able to edit all "Nike" shoes.
- So you create an ACF User relationship field type in the "brand" post type, and you can assign multiple users to admin that brand.
- So, when that user logs into the back-end and views all shoes, I want just Nikes shoes to show up and they will have the ability to edit all Nike shoes.
I've built out the logic to handle this on the front-end and it works perfect, and now I'd like to duplicate this on the back-end for filtering there as well. Here's what I built out on the front-end for reference:
$current_user = wp_get_current_user();
/* Get All Brands the Current User can Administer */
$brands_admin_args = array(
'post_type' => 'brand',
'meta_query' => array(
array(
'key' => 'admins', // This is my ACF User field that tracks brand admins.
'value' => '"' . $current_user->ID . '"',
'compare' => 'LIKE'
),
),
);
$brands_admin = get_posts($brands_admin_args);
/* Get All Shoes The User Can Administer Based On Each Brand They Can Administer*/
$approved_shoes_args = array(
'post_type' => 'shoe',
'meta_query' => array(
'relation' => 'OR'
),
);
foreach ($brands_admin as $ba) {
array_push($approved_shoes_args['meta_query'], array(
'key' => 'shoe_brands', // This is my ACF Relationship field between Shoes and Brands
'value' => '"' . $ba->ID . '"',
'compare' => 'LIKE'
));
}
$approved_shoes = new WP_Query($approved_shoes_args);