最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get Post Meta from ID in functions.php

programmeradmin1浏览0评论

I have the following in my theme's functions.php script.

function add_to_system( $appointment_id ) { 
    $name = get_post_meta( $appointment_id, '_appointment_guest_name', TRUE ); 
    $email = get_post_meta( $appointment_id, '_appointment_guest_email', TRUE ); 
    $telephone = get_post_meta( $appointment_id, '_cf_meta_value', TRUE ); 
    mail( "[email protected]","Test Email", $name.$email.$telephone.$appointment_id );
}
add_action( 'booked_new_appointment_created', 'add_to_system', 99, 1 );

However, when I test by making a booking, it just sends the appointment_id in the body of the email. It doesn't grab the post meta data associated with that ID. Am I doing something wrong here?

N.B. it's for this plugin: ;filter=all#comment_12081430.

I have the following in my theme's functions.php script.

function add_to_system( $appointment_id ) { 
    $name = get_post_meta( $appointment_id, '_appointment_guest_name', TRUE ); 
    $email = get_post_meta( $appointment_id, '_appointment_guest_email', TRUE ); 
    $telephone = get_post_meta( $appointment_id, '_cf_meta_value', TRUE ); 
    mail( "[email protected]","Test Email", $name.$email.$telephone.$appointment_id );
}
add_action( 'booked_new_appointment_created', 'add_to_system', 99, 1 );

However, when I test by making a booking, it just sends the appointment_id in the body of the email. It doesn't grab the post meta data associated with that ID. Am I doing something wrong here?

N.B. it's for this plugin: https://codecanyon/item/booked-appointments-appointment-booking-for-wordpress/9466968/comments?page=50&filter=all#comment_12081430.

Share Improve this question asked Jul 3, 2019 at 6:31 michaelmcgurkmichaelmcgurk 1134 bronze badges 6
  • 1 Do a var_dump( get_post_meta( $appointment_id ) ); in the function and see if you're actually using the correct meta keys. Some data like telephone number might also be empty. – Sally CJ Commented Jul 3, 2019 at 8:12
  • That pulls the data as expected but it doesn't in functions.php :( – michaelmcgurk Commented Jul 3, 2019 at 8:28
  • Where did you add the var_dump()? – Sally CJ Commented Jul 3, 2019 at 8:42
  • I created a separate file in my root called test.php with the following code: require ('./wp-blog-header.php'); $appointment_id = "4770"; $name = get_post_meta($appointment_id,'_appointment_guest_name',true); echo $name; – michaelmcgurk Commented Jul 3, 2019 at 8:43
  • Maybe the hook no longer exists.. try var_dump( did_action( 'booked_new_appointment_created' ) ); in that test file, right after the require line. What do you see? – Sally CJ Commented Jul 3, 2019 at 8:46
 |  Show 1 more comment

1 Answer 1

Reset to default 0

get_post_meta( $appointment_id, '_appointment_guest_name', TRUE );

please make sure that $appointment_id will be the post id and meta_key name should be proper with that id you can check this in wp_postmeta table(check meta_key name is _appointment_guest_name or appointment_guest_name)

发布评论

评论列表(0)

  1. 暂无评论