admin管理员组文章数量:1305525
Could you please let me know wordpress action hook that must triggered only once when a user create a new custom post.
That means if user creates a new post that hook must be triggered but when user update or edit the post then that hook should not be executed.
Previously i was using publish_post but it always executed when user create, edit or update the post however i have added some conditions to prevent this action but didn't works for me.
The code is as follow:
add_action( 'publish_property', 'pms_post_published_notification', 10, 2 );
function pms_post_published_notification( $ID, $post ) {
if(get_post_field('post_type',$ID ) === 'property' && !wp_is_post_revision( $ID )){// custom code here}
Could you please let me know wordpress action hook that must triggered only once when a user create a new custom post.
That means if user creates a new post that hook must be triggered but when user update or edit the post then that hook should not be executed.
Previously i was using publish_post but it always executed when user create, edit or update the post however i have added some conditions to prevent this action but didn't works for me.
The code is as follow:
add_action( 'publish_property', 'pms_post_published_notification', 10, 2 );
function pms_post_published_notification( $ID, $post ) {
if(get_post_field('post_type',$ID ) === 'property' && !wp_is_post_revision( $ID )){// custom code here}
Share
Improve this question
edited Feb 9, 2016 at 9:26
Sisir
7,8718 gold badges61 silver badges99 bronze badges
asked Feb 9, 2016 at 9:15
tinkutinku
1191 silver badge4 bronze badges
3 Answers
Reset to default 2You have to use new_to_publish
action as described on Post Status Transitions Codex page (updated according @prog comment):
add_action( 'new_to_publish', 'pms_post_published_notification', 10, 1 );
function pms_post_published_notification( $post ) {
if( 'property' === $post->post_type ) {
// custom code here
}
}
The only argument is passed to your function is $post
object, so you can utilize it to check the post type.
- Post Status Transitions
- {$old_status}_to_{$new_status} action
You can always make use of the more global transition_post_status
action hook which fires on every transition status for all post types.
Three values are passed by reference to the hook
$new_status
which is the post status the post will have after transition$old_status
which is the post status the post had before transitioning$post
which is the current post object of the post being transitioned
Because a new post does not have any concrete set old post status, and definitely does not have an old status of publish, we can use that as logic to test against and then run our custom code based on that
add_action( 'transition_post_status', function ( $new_status, $old_status, $post )
{
if ( 'publish' === $new_status
&& 'publish' !== $old_status
&& 'property' === $post->post_type
) {
// This is a new property post, lets do some work
// Run your custom code here
}
}, 10, 3 );
However the below given code works for me.
add_action( 'new_to_publish', 'pms_post_published_notification', 10, 1 );
function pms_post_published_notification( $post ) {
if($post->post_type == 'property') {
// custom code here
}
}
Thanks @Max
本文标签: Action hook on only publishing post not on editing or updating
版权声明:本文标题:Action hook on only publishing post not on editing or updating 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741803830a2398379.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论