admin管理员组文章数量:1336346
I think I've read every suggested article on this topic and there are some similar but nothing seems to solve my problem. It seems like it should be simple.
I just want to be able to use get_post_meta($id)
where $id
was set earlier in the script or even passed into the function. Whenever I do this I get only meta about the _edit_lock
and _last_edit
.
If I hardcode the post id in as say 2985 it works fine, but obviously that does not do what I need as the post will change. For those that want to know I am triggering on the publish of a post of one custom post type to take the shortcode from that type and insert it into a different custom post type.
I am hoping that one of you brilliant people out there can help a relative newbie out here.
Here is my code:
add_action('transition_post_status', 'dif_create_zoom_mtg_lesson', 10, 3);
function dif_create_zoom_mtg_lesson ($new_status, $old_status, $postobj) {
if('publish' === $new_status && 'publish' !== $old_status && $postobj->post_type === 'zoom-meetings') {
global $zoom;
global $post;
ob_start();
echo "POST IN ENTIRETY -metas"."<br/>";
var_dump($postobj);
$getpostid = $postobj->ID;
echo "postid ==>";
var_dump($getpostid);
echo "<br/>";
$postmetas = get_post_meta($getpostid);
echo "VARDUMP postmetas===>";
var_dump($postmetas);
echo "<br/>";
$dif_zoom_mtg_body .= ob_get_clean();
$dif_countdown_timer = '<p id="dif-timer"></p>';
$dif_zoom_mtg_body .= $dif_countdown_timer;
// not working now since I cannot get meeting_id dynamically
//$dif_zoom_mtg_body .= '[zoom_api_link meeting_id="'.$dif_zoom_mtg_id.'" link_only="no"]';
$my_post = array();
$my_post['post_type'] = 'post';
$my_post['post_title'] = "Zoom post ID=".$getpostid;
$my_post['post_content'] = $dif_zoom_mtg_body;
$my_post['post_status'] = 'publish';
$my_post['post_author'] = 1;
$my_post['post_category'] = array(0);
// Insert the post into the database
wp_insert_post( $my_post );
}
}
This gives me the output of:
POST IN ENTIRETY -metas
object(WP_Post)#4361 (24) {
["ID"]=>
int(2733)
["post_author"]=>
string(1) "1"
["post_date"]=>
string(19) "2020-06-11 16:44:55"
["post_date_gmt"]=>
string(19) "2020-06-11 16:44:55"
["post_content"]=>
string(61) "Xcar body
["post_title"]=> string(5) "X car"
["post_excerpt"]=> string(0) ""
["post_status"]=> string(7) "publish"
["comment_status"]=> string(6) "closed"
["ping_status"]=> string(6) "closed"
["post_password"]=> string(0) ""
["post_name"]=> string(5) "x-car"
["to_ping"]=> string(0) ""
["pinged"]=> string(0) ""
["post_modified"]=> string(19) "2020-06-11 16:44:55"
["post_modified_gmt"]=> string(19) "2020-06-11 16:44:55"
["post_content_filtered"]=> string(0) ""
["post_parent"]=> int(0)
["guid"]=> string(56) "/?post_type=zoom-meetings&p=2733"
["menu_order"]=> int(0)
["post_type"]=> string(13) "zoom-meetings"
["post_mime_type"]=> string(0) ""
["comment_count"]=> string(1) "0"
["filter"]=> string(3) "raw"
}
postid ==>int(2733)
VARDUMP postmetas===>array(1) {
["_edit_lock"]=>
array(1) {
[0]=>
string(12) "1591893865:1"
}
}
I think I've read every suggested article on this topic and there are some similar but nothing seems to solve my problem. It seems like it should be simple.
I just want to be able to use get_post_meta($id)
where $id
was set earlier in the script or even passed into the function. Whenever I do this I get only meta about the _edit_lock
and _last_edit
.
If I hardcode the post id in as say 2985 it works fine, but obviously that does not do what I need as the post will change. For those that want to know I am triggering on the publish of a post of one custom post type to take the shortcode from that type and insert it into a different custom post type.
I am hoping that one of you brilliant people out there can help a relative newbie out here.
Here is my code:
add_action('transition_post_status', 'dif_create_zoom_mtg_lesson', 10, 3);
function dif_create_zoom_mtg_lesson ($new_status, $old_status, $postobj) {
if('publish' === $new_status && 'publish' !== $old_status && $postobj->post_type === 'zoom-meetings') {
global $zoom;
global $post;
ob_start();
echo "POST IN ENTIRETY -metas"."<br/>";
var_dump($postobj);
$getpostid = $postobj->ID;
echo "postid ==>";
var_dump($getpostid);
echo "<br/>";
$postmetas = get_post_meta($getpostid);
echo "VARDUMP postmetas===>";
var_dump($postmetas);
echo "<br/>";
$dif_zoom_mtg_body .= ob_get_clean();
$dif_countdown_timer = '<p id="dif-timer"></p>';
$dif_zoom_mtg_body .= $dif_countdown_timer;
// not working now since I cannot get meeting_id dynamically
//$dif_zoom_mtg_body .= '[zoom_api_link meeting_id="'.$dif_zoom_mtg_id.'" link_only="no"]';
$my_post = array();
$my_post['post_type'] = 'post';
$my_post['post_title'] = "Zoom post ID=".$getpostid;
$my_post['post_content'] = $dif_zoom_mtg_body;
$my_post['post_status'] = 'publish';
$my_post['post_author'] = 1;
$my_post['post_category'] = array(0);
// Insert the post into the database
wp_insert_post( $my_post );
}
}
This gives me the output of:
Share Improve this question edited Jun 12, 2020 at 4:13 Mort 1305 9835 silver badges18 bronze badges asked Jun 11, 2020 at 16:58 a2guya2guy 11 bronze badgePOST IN ENTIRETY -metas
object(WP_Post)#4361 (24) {
["ID"]=>
int(2733)
["post_author"]=>
string(1) "1"
["post_date"]=>
string(19) "2020-06-11 16:44:55"
["post_date_gmt"]=>
string(19) "2020-06-11 16:44:55"
["post_content"]=>
string(61) "Xcar body
["post_title"]=> string(5) "X car"
["post_excerpt"]=> string(0) ""
["post_status"]=> string(7) "publish"
["comment_status"]=> string(6) "closed"
["ping_status"]=> string(6) "closed"
["post_password"]=> string(0) ""
["post_name"]=> string(5) "x-car"
["to_ping"]=> string(0) ""
["pinged"]=> string(0) ""
["post_modified"]=> string(19) "2020-06-11 16:44:55"
["post_modified_gmt"]=> string(19) "2020-06-11 16:44:55"
["post_content_filtered"]=> string(0) ""
["post_parent"]=> int(0)
["guid"]=> string(56) "https://xxxxxxx/?post_type=zoom-meetings&p=2733"
["menu_order"]=> int(0)
["post_type"]=> string(13) "zoom-meetings"
["post_mime_type"]=> string(0) ""
["comment_count"]=> string(1) "0"
["filter"]=> string(3) "raw"
}
postid ==>int(2733)
VARDUMP postmetas===>array(1) {
["_edit_lock"]=>
array(1) {
[0]=>
string(12) "1591893865:1"
}
}
1 Answer
Reset to default 1If you're hooking into publish_post
, that fires before meta is ready.
Either get the meta from the POST object, or use a different hook - possibly update_post_meta.
本文标签: getpostmeta not working when variable used for post ID
版权声明:本文标题:get_post_meta not working when variable used for post ID 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742356106a2459444.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论