0

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!

0

Can you check the argument passed $post_id and see if it is returning the post id else define post globally along with $wpdb like global $wpdb, $post and use $post->ID instead of $post_id.

1
  • I included $post_id in the email and it outputted just fine. I also tried to use global $post, still no luck. – sublimelaconic Sep 26 '13 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.