WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question

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.

share|improve this answer
I included $post_id in the email and it outputted just fine. I also tried to use global $post, still no luck. – user1526570 Sep 26 '13 at 9:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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