Change WordPress user roles and capabilities Forums How to or FAQ Set Posts to manage at User level but by an ACF setting Reply To: Set Posts to manage at User level but by an ACF setting

#6717
Vladimir
Keymaster

Erik,

I updated custom code at functions.php to this version:


function fetch_analytics_posts(){
	global $post; 

	$current_user = wp_get_current_user();
	$ag = get_field('analytics_group_name','user_' . $current_user->ID);
	if ( !isset($ag) ) {
		return '';
	}
	
	$posts = get_posts(array(
        'suppress_filters'  => true,    
	'numberposts'	=> -1,
	'post_type'		=> 'page',
	'meta_query'	=> array(
		'relation'		=> 'OR',
		array(
			'key'	 	=> 'analytics_group_name',
			'value'	  	=> $ag,
			'compare' 	=> 'IN',
			),		
		),
	));

	foreach($posts as $post){
		$list .= $post->ID.',';
	}

	$list = rtrim($list, ',');

	wp_reset_postdata();

	// echo 'allowed posts:'. $list;
	return $list;
}

add_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list_cu', 10, 1);

function ure_edit_posts_access_set_id_list_cu($list) {
	
        remove_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list_cu', 10);
        $ag_posts_list = fetch_analytics_posts();
        add_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list_cu', 10, 1);
        
	if ( empty( $ag_posts_list ) ) {
		return $list;
	}
	
	if ( empty( $list ) ) {
		return $ag_posts_list;
	}
	
	$list .= ', '. $ag_posts_list;
	
	return $list;
}

I tested with existing user with ‘Program Editor’ role. When I turn ON SBU002 analytics group for her, she sees 30 pages only. (Pay attention that some pages with empty analytics group field were returned by your get_posts() query).