admin管理员组文章数量:1130349
I am trying to figure out to fire a hook for a custom post type called "bananas" when a new bananas post is published.
Requirements:
- Can't use $_POST
- Need to be able to get post statuses so I can prevent code from running again later and check if its already published
- Need to be able to get_post_meta
The action hook is working perfectly. The only issue is that NEW post can't seem to get_post_meta. If you go from pending to publish or vice versa getting the meta works. But getting the meta on new post does not work and returns an empty result.
Heres an example of what I am trying to do.
class bananas {
public function __construct() {
add_action( 'transition_post_status', array( $this, 'email_bananas_published' ), 10, 3 );
}
public function email_bananas_published( $new_status, $old_status, $post ) {
if ( $post->post_type === 'bananas' && $new_status !== $old_status ) {
$email = get_post_meta( $post->ID, '_bananas_email', true );
error_log( $email );
}
}
}
I have been stuck on this for a while and any help is much appreciated.
I am trying to figure out to fire a hook for a custom post type called "bananas" when a new bananas post is published.
Requirements:
- Can't use $_POST
- Need to be able to get post statuses so I can prevent code from running again later and check if its already published
- Need to be able to get_post_meta
The action hook is working perfectly. The only issue is that NEW post can't seem to get_post_meta. If you go from pending to publish or vice versa getting the meta works. But getting the meta on new post does not work and returns an empty result.
Heres an example of what I am trying to do.
class bananas {
public function __construct() {
add_action( 'transition_post_status', array( $this, 'email_bananas_published' ), 10, 3 );
}
public function email_bananas_published( $new_status, $old_status, $post ) {
if ( $post->post_type === 'bananas' && $new_status !== $old_status ) {
$email = get_post_meta( $post->ID, '_bananas_email', true );
error_log( $email );
}
}
}
I have been stuck on this for a while and any help is much appreciated.
Share Improve this question edited Aug 1, 2016 at 21:03 JediTricks007 asked Aug 1, 2016 at 20:51 JediTricks007JediTricks007 8464 gold badges14 silver badges27 bronze badges 1 |2 Answers
Reset to default 3The following worked for me. I hooked my meta saving and retrieving to the same action (post_transition_status), but with different priorities.
//Save Your Custom Meta Meta, but hook it to transition_post_status instead of save_post, with a priority of 1
function save_yourpost_meta(){
global $post;
if($post -> post_type == 'your_post_type') {
update_post_meta($post->ID, "your_meta_key", $_POST["your_meta_key"]);
}
}
add_action('transition_post_status', 'save_yourpost_meta',1,1);
//Then retrieve your custom meta only when the post is saved for the first time, not on updates. Hooked to the same action, with a lower priority of 100
function get_meta_on_publish($new_status, $old_status, $post) {
if('publish' == $new_status && 'publish' !== $old_status && $post->post_type == 'your_post_type') {
//Get your meta info
global $post;
$postID = $post->ID;
$custom = get_post_custom($postID);
//You have your custom now, have fun.
}
}
add_action('transition_post_status', 'get_meta_on_publish',100,3);
I hope this helps!
Have you checked wp_insert_post hook in the codex?
I believe this is what you are looking for. (untested)
class bananas {
public function __construct() {
add_action( 'wp_insert_post', array( $this, 'email_bananas_published' ), 10, 3 );
}
public function email_bananas_published( $post_id, $post, $update ) {
// If this is a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) || $update )
return;
if ( $post->post_type === 'bananas' && $post->post_status === 'publish' ) {
$email = get_post_meta( $post_id, '_bananas_email', true );
error_log( $email );
}
}
}
I am trying to figure out to fire a hook for a custom post type called "bananas" when a new bananas post is published.
Requirements:
- Can't use $_POST
- Need to be able to get post statuses so I can prevent code from running again later and check if its already published
- Need to be able to get_post_meta
The action hook is working perfectly. The only issue is that NEW post can't seem to get_post_meta. If you go from pending to publish or vice versa getting the meta works. But getting the meta on new post does not work and returns an empty result.
Heres an example of what I am trying to do.
class bananas {
public function __construct() {
add_action( 'transition_post_status', array( $this, 'email_bananas_published' ), 10, 3 );
}
public function email_bananas_published( $new_status, $old_status, $post ) {
if ( $post->post_type === 'bananas' && $new_status !== $old_status ) {
$email = get_post_meta( $post->ID, '_bananas_email', true );
error_log( $email );
}
}
}
I have been stuck on this for a while and any help is much appreciated.
I am trying to figure out to fire a hook for a custom post type called "bananas" when a new bananas post is published.
Requirements:
- Can't use $_POST
- Need to be able to get post statuses so I can prevent code from running again later and check if its already published
- Need to be able to get_post_meta
The action hook is working perfectly. The only issue is that NEW post can't seem to get_post_meta. If you go from pending to publish or vice versa getting the meta works. But getting the meta on new post does not work and returns an empty result.
Heres an example of what I am trying to do.
class bananas {
public function __construct() {
add_action( 'transition_post_status', array( $this, 'email_bananas_published' ), 10, 3 );
}
public function email_bananas_published( $new_status, $old_status, $post ) {
if ( $post->post_type === 'bananas' && $new_status !== $old_status ) {
$email = get_post_meta( $post->ID, '_bananas_email', true );
error_log( $email );
}
}
}
I have been stuck on this for a while and any help is much appreciated.
Share Improve this question edited Aug 1, 2016 at 21:03 JediTricks007 asked Aug 1, 2016 at 20:51 JediTricks007JediTricks007 8464 gold badges14 silver badges27 bronze badges 1-
Post meta is not available yet for new posts of autosaves. You will have to make sure you handle your post meta before running email_bananas_published. For this, you'll have to use
$_POSTas suggested in the answer by Palmer Del Campo below. – RavanH Commented Apr 19, 2019 at 16:57
2 Answers
Reset to default 3The following worked for me. I hooked my meta saving and retrieving to the same action (post_transition_status), but with different priorities.
//Save Your Custom Meta Meta, but hook it to transition_post_status instead of save_post, with a priority of 1
function save_yourpost_meta(){
global $post;
if($post -> post_type == 'your_post_type') {
update_post_meta($post->ID, "your_meta_key", $_POST["your_meta_key"]);
}
}
add_action('transition_post_status', 'save_yourpost_meta',1,1);
//Then retrieve your custom meta only when the post is saved for the first time, not on updates. Hooked to the same action, with a lower priority of 100
function get_meta_on_publish($new_status, $old_status, $post) {
if('publish' == $new_status && 'publish' !== $old_status && $post->post_type == 'your_post_type') {
//Get your meta info
global $post;
$postID = $post->ID;
$custom = get_post_custom($postID);
//You have your custom now, have fun.
}
}
add_action('transition_post_status', 'get_meta_on_publish',100,3);
I hope this helps!
Have you checked wp_insert_post hook in the codex?
I believe this is what you are looking for. (untested)
class bananas {
public function __construct() {
add_action( 'wp_insert_post', array( $this, 'email_bananas_published' ), 10, 3 );
}
public function email_bananas_published( $post_id, $post, $update ) {
// If this is a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) || $update )
return;
if ( $post->post_type === 'bananas' && $post->post_status === 'publish' ) {
$email = get_post_meta( $post_id, '_bananas_email', true );
error_log( $email );
}
}
}
本文标签: Custom Post Status Transition Issues With Get Post Meta
版权声明:本文标题:Custom Post Status Transition Issues With Get Post Meta 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749119655a2318784.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


$_POSTas suggested in the answer by Palmer Del Campo below. – RavanH Commented Apr 19, 2019 at 16:57