Defaulting new posts (only) to use “Registered” group

Posted in

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 ) );
}

3 Responses to Defaulting new posts (only) to use “Registered” group

  1. George October 6, 2020 at 1:42 pm #

    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

  2. Martin Whittaker October 6, 2020 at 8:15 am #

    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

  3. George October 5, 2020 at 9:37 pm #

    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

We use cookies to optimize your experience on our site and assume you're OK with that if you stay.
OK, hide this message.

Affiliates · Contact · Jobs · Terms & Conditions · Privacy Policy · Documentation · Downloads · Useful Plugins · My Account

Share