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 have also tried using global $post;
and using $post->ID
as the ID. I have also tried getting the terms, and it displays fine. I'm just having trouble displaying the meta.
Any alternative solutions are welcome. Thanks!
echo $event_date
?$message
? Also, I tried adding an if-else statement which displays the word "Empty" if$event_date
is empty. And it shows "Empty" T_Tget_the_...
statements only return the value. Something isn't adding up...when I get by my computer I'll try and help some more.do_action('publish_th_events', ....
'publish_th_events'
is passing a post object, not a post ID.