Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save champsupertramp/fac9d4c0f2499d055724126d8fa68272 to your computer and use it in GitHub Desktop.
Save champsupertramp/fac9d4c0f2499d055724126d8fa68272 to your computer and use it in GitHub Desktop.
Ultimate Member 2.3 - Mass update to add new role to content restrictions in all posts
add_action("template_redirect","um_custom_add_new_role_posts_restriction");
function um_custom_add_new_role_posts_restriction(){
$p_query = new WP_Query;
$posts = $p_query->query( array(
'post_type' => 'any',
'posts_per_page' => -1,
'fields' => 'ids'
) );
if ( ! empty( $posts ) ) {
foreach ( $posts as $post_id ) {
$access_roles = array();
$content_restriction = get_post_meta( $post_id, 'um_content_restriction', true );
if( empty( $content_restriction ) ) continue;
// enter new role slugs to add in the content restriction
$new_roles = [
'board_member' => 1,
'tier_one' => 1,
];
$existing_roles = [];
if( ! empty( $content_restriction['_um_access_roles'] ) ){
$existing_roles = $content_restriction['_um_access_roles'];
}
//if( $existing_roles["subscriber"] == 1 ){ add the new roles when specific subscriber exist to the content restriction settings
$content_restriction['_um_access_roles'] = array_merge( $new_roles, $existing_roles );
// }
update_post_meta( $post_id, 'um_content_restriction', $content_restriction );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment