• Resolved julienta

    (@julienta)


    Hello, I would like to have the name of the group when I save a post with ACF with the action acf/save_post. Here is the function I use to send the mail. The email is sent but the $name variable is empty.

    How to find the information of the group linked to the post please. Thanks a lot.

    
    add_action('acf/save_post' , 'my_save_post', 15);
    function my_save_post($post_id) {
    
        add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
    
        $post       = get_post($post_id);
        $post_url   = get_permalink($post_id);
        $post_title = get_the_title($post_id); 
        $author     = get_userdata($post->post_author);
        $subject    = 'Post publish notification - post id : ' . $post_id;
        $sendmail   = get_field('send_email_notification', $post_id);
    
        $group_ids = Groups_Post_Access:: get_read_group_ids( $post_id );
        $group = new Groups_Group( $group_id );
        $name = $group->name;
    
        $message    = "This message was sent to the following group:" . $name;
                
        $users = get_users( array( 'role' => 'administrator' ) );
        if( ! empty( $users ) ) {
            $emails = wp_list_pluck( $users, 'user_email' );
        }
    
        if  ( ($sendmail == '0') || (empty($sendmail)) ) {
    
        } else {
    
            wp_mail($emails, $subject, $message );
            
        }
    
        remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
    
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi there Julienta,

    Good evening.

    Looking at your code above, two errors can be noticed:
    1. $group_ids = Groups_Post_Access:: get_read_group_ids( $post_id ); should be $group_ids = Groups_Post_Access::get_read_group_ids( $post_id );. You had a trailing space there.

    2. On this line: $group = new Groups_Group( $group_id ); notice the variable $group_id doesn’t exist. You can replace that line with $group = new Groups_Group( $group_ids[0] ); instead, considering the retrieved group id will be stored in the $group_ids variable you created.

    It is also worth mentioning that Groups_Post_Access:: get_read_group_ids( $post_id ); will return an array of group ids, hence if a post has more than on group assigned to it, you would need to loop over the array to generate the group names.

    However, if you are certain the post will only have one group assigned to it then it suffices to use $group_ids[0] to access the group id only.

    STAY SAFE and I wish you a very fine rest of your day!
    Best,
    Eugen.

    Thread Starter julienta

    (@julienta)

    Hi Eugen Bleck,

    Thank you for this feedback. I’m really sorry for these errors. However, variables always return 1. I can’t get the group id.

    Thank you,

    Julienta

    Hi Julienta,

    Good morning. I trust you are well and safe!

    Thank you for the feedback.

    Below is the code that retrieves the group id and renders the group name:

    add_action('acf/save_post' , 'my_save_post', 15);
    function my_save_post($post_id) {
    
        add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
    
        $post       = get_post($post_id);
        $post_url   = get_permalink($post_id);
        $post_title = get_the_title($post_id); 
        $author     = get_userdata($post->post_author);
        $subject    = 'Post publish notification - post id : ' . $post_id;
        $sendmail   = get_field('send_email_notification', $post_id);
    
        $group_ids = Groups_Post_Access::get_read_group_ids( $post_id );
    
        $group = new Groups_Group( $group_ids[0] );
        $name = $group->name;
    
        $message    = "This message was sent to the following group:" . $name;
    }

    You can use error_log( print_r($message, TRUE )); to see the computed message string with group name present. You would see this in your debug.log.

    I would, however, recommend you take a closer look at the code involved in sending the email and ensure it is working as it should.

    STAY SAFE and I wish you a very productive day ahead!
    Best,
    Eugen.

    Thread Starter julienta

    (@julienta)

    Thank you very much Eugene.

    I changed my code snippet. I did a test and this is what i get in my log:

    [27-Jul-2022 12:11:58 UTC] PHP Warning: Undefined array key 0 in /site/wp-content/themes/theme/functions.php

    for this line $group = new Groups_Group( $group_ids[0] );

    [27-Jul-2022 12:11:58 UTC] PHP Warning: Attempt to read property “name” on bool in /site/wp-content/plugins/groups/lib/core/class-groups-group.php on line 78

    [27-Jul-2022 12:12:01 UTC] This message was sent to the following group:

    I use version 2.16.2 of the plugin.

    Julienta

    Hi Julienta,

    Good afternoon. Thanks for sharing the feedback.

    If $group_ids[0] isn’t accessible then it means the post, isn’t restricted by a group. You can verify this by editing the post in question and checking the Groups setting to verify if a group has been assigned to the post or not.

    .

    It is also worth mentioning that if you are using the acf_form() to update the post then you have to ensure the following:

    acf_form(array(
            'post_id'       => get_the_ID(),
            'field_groups'	=> array( 11 ),
            'post_title'    => false,
            'post_content'  => false,
            'submit_value'  => __('Update meta')
        ));

    Where 11, is the ID of the field group to be updated.

    You can read more on acf_form() here.

    STAY SAFE and I wish you a very productive day ahead!
    Best,
    Eugen.

    Thread Starter julienta

    (@julienta)

    Hi Eugen,

    I used updated_post_meta instead of acf/save_post and it works. Thanks for your help.

    Julienta

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Get group name in acf/save_post action’ is closed to new replies.