Your cart is currently empty!
Defaulting new posts (only) to use “Registered” group
—
by
Hi,
Great plugin – thanks
I’m trying to default to “registered” group on save of post. Adding the code you mentioned in a reply to someone else it saves for both posts and pages, I’m looking for posts only. This is my staring point https://github.com/eggemplo/Groups-Utilities/blob/master/post-default-group.php in functions.php
Any help would be much appreciated.
Thanks
// This code add the group with id 160 as selected by default.
// Change the 160 to your group_id
// If you have selected the legacy mode, please use the code: https://github.com/eggemplo/Groups-Utilities/blob/master/post-default-capability.php
add_action( ‘groups_access_meta_boxes_after_groups_read_update’, ‘my_groups_access_meta_boxes_after_groups_read_update’, 10, 3);
function my_groups_access_meta_boxes_after_groups_read_update ( $post_id, $group_ids, $update_result ) {
$group_ids[] = 160;
Groups_Post_Access::update( array( ‘post_id’ => $post_id, ‘groups_read’ => $group_ids ) );
}
Comments
3 responses to “Defaulting new posts (only) to use “Registered” group”
Hey Martin,
Yes that’s true, you’re already in the post so you can check the post_type the way you did, great!
Kind regards,
George
Many thanks – that works well
I had:
add_action( 'groups_access_meta_boxes_after_groups_read_update', 'my_groups_access_meta_boxes_after_groups_read_update', 10, 3);
function my_groups_access_meta_boxes_after_groups_read_update ( $post_id, $group_ids, $update_result ) {
if ( 'post' == get_post_type() ) {
$group_ids[] = 160;
Groups_Post_Access::update( array( 'post_id' => $post_id, 'groups_read' => $group_ids ) );
}
}
I guess this worked because post_id was already known.
Thanks again – much appreciated
Martin
Hi Martin,
Welcome to our support forum and many thanks for using our tools on your site.
An easy way to check the post type would be by getting the WP_Post object by the post_id found in the hook arguments and then check the post_type property of the obj if it’s a post, then update the groups access.
You can use something like this:
add_action( 'groups_access_meta_boxes_after_groups_read_update', 'my_groups_access_meta_boxes_after_groups_read_update', 10, 3);
function my_groups_access_meta_boxes_after_groups_read_update ( $post_id, $group_ids, $update_result ) {
$group_ids[] = 160;
$the_post = get_post( $post_id );
if ( $the_post && $the_post->post_type == 'post' ) {
Groups_Post_Access::update( array( 'post_id' => $post_id, 'groups_read' => $group_ids ) );
}
}
Kind regards,
George