Change WordPress user roles and capabilities › Forums › How to or FAQ › Set Posts to manage at User level but by an ACF setting
- This topic has 13 replies, 2 voices, and was last updated 4 years, 8 months ago by edering.
-
AuthorPosts
-
01/03/2020 at 17:58 #6671ederingParticipant
We have an Advanced Custom Field group that allows us to set Pages/Posts by a Team number (Team 1, Team 2, Team 3 etc). We will be creating a limited role for editing pages/posts, and want to know if we can use this when creating a User that is set to that Role, like you offer for by Page/Post ID Restrictions. This way, we would not have to find all of the Page/Post IDs for each team, especially because the pages that Teams are set for change sometimes. Is this possible?
04/03/2020 at 03:40 #6672VladimirKeymasterIf you can extract needed post/page IDs for a team via PHP, you can add them automatically to the edit restrictions set for selected user or role using ure_edit_posts_access_id_list custom filter.
23/03/2020 at 11:57 #6702ederingParticipantHi Vladimir,
I took your recommendation above and to use the ure_edit_posts_access_id_list function and created this:
add_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list_cu'); function ure_edit_posts_access_set_id_list_cu($list) { $current_user = wp_get_current_user(); $ag = get_field('analytics_group_name','user_' . $current_user->ID); if(!isset($ag)){ return; }else{ $posts = get_posts(array( '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(); return $list; } }
I’m able to return a list via echo and can see my comma separated list in the admin at the top of the page if I use init() temporarily. However, when I attempt to create a page (or any post type) as one of the role types that have a restriction by custom field, the page eventually runs out of memory (currently set to 1G) and we receive several fatal errors:
https://www.dropbox.com/s/wa0x9v892xrf0ua/server-errors.jpg?dl=0This role has what I believe to be everything needed for this role to create a post type:
create_pages
create_posts
create_tablepress_tables
create_tribe_eventsOther roles that we have set up do not have this issue, only this one. Any thoughts or recommendations on what we can try to resolve this issue?
24/03/2020 at 01:56 #6703VladimirKeymaster1st of all, filter function should always return value.
Replaceif (!isset($ag)) { return; } else {
with
if (!isset($ag)) { return $list; } else {
Is it too much posts (approximate quantity) has analytics group name, for which you get out of memory error?
24/03/2020 at 02:02 #6704ederingParticipantVladimir,
Thanks for the response. I had it return early in the even tthat there the were no values assigned to the custom field. Should I not do that? Also, there are ~500 pages on the site. There are likely occasions that nearly all could be returned. Would that cause this to time out?
24/03/2020 at 03:05 #6705VladimirKeymasterYes, your function as filter should always return value. If there is nothing to change, then return unchanged value it got as input parameter.
500 is not a problem. There are installations with thousands posts and/or pages, which work fine.
Try to turn ON wordpress debug mode with writing to file. Add this lines to wp-config.php for that:
define('WP_DEBUG', true); define('WP_DEBUG_LOG', true);
WordPress will write all error messages to wp-content/debug.log file in this case. Reproduce the error.
Look if ‘user-role-editor-pro’ is mentioned at the error messages.Another variant for investigation:
Strange, that out of memory error is raised at different times from different sources. Can you try to deactivate all plugins except URE and test again. In case error will go away, activate plugins back one by one until error will return. Just in case some other plugin eats to much memory.24/03/2020 at 03:16 #6706ederingParticipantThanks. I have debugging turned on and the only errors logged are the ones are showing are the ones I’ve pasted above:
https://www.dropbox.com/s/wa0x9v892xrf0ua/server-errors.jpg?dl=0I’ll try turning off plugins and making the return adjustment above.
24/03/2020 at 05:34 #6707VladimirKeymasterAnother suggestion –
You use get_posts() inside your filter function. URE’s uses ‘pre_get_posts’ hook to define the list of the allowed posts, thus calling get_posts() inside filter function may lead to the endless recursion and finally to the problem with available memory. So try to switch off this custom filter before to call get_posts(), then restore it back, like this:remove_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list_cu'); $posts = get_posts(array( 'numberposts' => -1, 'post_type' => 'page', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'analytics_group_name', 'value' => $ag, 'compare' => 'IN', ), ), )); add_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list_cu');
If this will not help, let me know, we will try to switch off/restore directly URE’s global post edit access filter.
24/03/2020 at 12:09 #6709ederingParticipantVladimir,
I’ve determined that having the get_posts loop inside the function is causing issues, however, if I take it outside of the function, I can no longer access the values of the loop. Am I missing something?
24/03/2020 at 13:37 #6710ederingParticipantVladimir, I create an Admin user for you. Please look for an email to set your password. I am hoping you having access to the functions file and seeing where people assign the Analytics Group Name to a User will help you.
25/03/2020 at 02:50 #6713VladimirKeymasterErik,
I moved get_posts() out of filter function and saved its result inside global variable. So filter function looks now as:
function ure_edit_posts_access_set_id_list_cu($list) { global $ag_posts_list; if ( empty( $ag_posts_list ) ) { return $list; } if ( empty( $list ) ) { return $ag_posts_list; } $list .= ', '. $ag_posts_list; return $list;
I created successfully new page ‘Test 124’ under user with ‘Program Editor’.
Make your own test and let me know the result.26/03/2020 at 22:57 #6716ederingParticipantVlad,
Thanks for your work on this. Unfortunately, the user group in question isn’t working. If you’re able, can you create a user with the role Program Editor and assign a
Analytics Group Name checkbox in the user profile? You should not be able to see all posts — only a limited number. The good news is that creating a post no longer fails, however, all posts should not show for non-admin roles. Are you able to take a look? Thanks for your help so far.27/03/2020 at 11:51 #6717VladimirKeymasterErik,
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).
29/03/2020 at 18:25 #6722ederingParticipantThank you! This is working great now!
-
AuthorPosts
- You must be logged in to reply to this topic.