Here I have custom post type called products
and it's taxonomy is product_categories
. What I need is whenever I add the post in products , I need to call function1, when I update the post then I need to call function2 . How to do this?
I have searched on google and found this solution:
add_action('save_post', 'save_in_filter', 10, 2);
function save_in_filter($post_id, $post){
function1();
}
function mynewproduct(){
myfunction();
}
But what happens is when I click on add new post then suddenly the function1() execute , but what i need is function1 execute after inserting the data not before inserting the data.
When a post is added to products (post_type=products)
i need to
execute function1()
. When a post is updated in products i need to
execute function2();
Why post_updated,save_post function not working properly?
Here I have custom post type called products
and it's taxonomy is product_categories
. What I need is whenever I add the post in products , I need to call function1, when I update the post then I need to call function2 . How to do this?
I have searched on google and found this solution:
add_action('save_post', 'save_in_filter', 10, 2);
function save_in_filter($post_id, $post){
function1();
}
function mynewproduct(){
myfunction();
}
But what happens is when I click on add new post then suddenly the function1() execute , but what i need is function1 execute after inserting the data not before inserting the data.
When a post is added to products (post_type=products)
i need to
execute function1()
. When a post is updated in products i need to
execute function2();
Why post_updated,save_post function not working properly?
3 Answers
Reset to default -1This is the original answer but it is little faulty, look below for the updated
You can use following approach.
The my_action_updated_post_meta
is executed after the post insert or update is done:
// define the updated_post_meta callback
function my_action_updated_post_meta( $array, $int, $int ) {
global $post;
// see your updated post
print_r($post);
// make your action here...
die('after update');
};
// add the action
add_action( 'updated_post_meta', 'my_action_updated_post_meta', 10, 3 );
Here's the function's documentation page.
Update
I have notice that the above response is a little faulty - the updated_post_meta
action (for post updates) is fired also when opening post for edition in the admin panel (since the _edit_lock
is set then), even when no real change is made then.
So I changed the approach.
I use a global variable $my_updated_flag
to know that a "real" change has been made. I set this variable to "1" by post_updated
action. So if $my_updated_flag == 1
we know that this is not just a _edit_lock
change. To differentiate post insert and update action I check the $post->post_status == 'draft'
since new posts have "draft" status at this stage.
$my_updated_flag = 0;
// define the updated_post_meta callback
function action_updated_post_meta( $meta_id, $post_id, $meta_key, $meta_value = '' ) {
global $post, $my_updated_flag;
if ($my_updated_flag == 1) {
if ($post->post_status == 'draft') {
die('post_inserted');
} else {
die('post_updated');
}
$my_updated_flag = 0;
}
};
add_action( 'updated_post_meta', 'action_updated_post_meta', 10, 4 );
function action_post_updated( $array, $int, $int ) {
global $my_updated_flag;
$my_updated_flag = 1;
};
add_action( 'post_updated', 'action_post_updated', 10, 3 );
have a look at the post_updated
hook for example..
// Hook to all private or public post types updating
add_action( 'post_updated', 'my_function' );
function my_function( $post_id ){
$post_status = get_post_status( $post_id );
switch ( $post_status ) {
case 'draft':
case 'auto-draft':
case 'pending':
case 'inherit':
case 'trash':
return;
case 'future':
case 'publish':
case 'private':
// continue
}
'do something;
}
You can check these links bellow:
https://wordpress.stackexchange/a/192323/68186
https://codex.wordpress/Plugin_API/Action_Reference/save_post
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744846437a4596865.html
评论列表(0条)