• I made this action hook which will send an email to the users every time a new post in the custom post type “events” is made.

    function email_members($post_id) {
    global $wpdb;
    
    $usersarray = $wpdb->get_col("SELECT user_email FROM wp_users");
    $users = implode(",", $usersarray);
    
    if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
        $subject = 'Event Reminder';
    
        $post_url = get_permalink($post_id);
        $post_title = get_the_title($post_id);
        $event_date = get_post_meta( $post_id, 'ch_event_date', true );
    
        $message = "APAC Ministries Event Reminder for: ".$post_title."\n\n";
        $message .= "Date: ".$event_date;
    
        wp_mail($users, $subject, $message );
      }
    }
    
    add_action('publish_th_events', 'email_members');

    However, the $event_date is always empty. I tried running this code in a different file and it works:

    global $post;
    $test = get_post_meta($post->ID, 'ch_event_date', true);
    echo $test;

    I can’t seem to use get_post_meta in wp_mail.

    Any alternative solutions are welcome. Thanks!

  • The topic ‘get_post_meta is always empty when I use wp_mail’ is closed to new replies.