I am using the save_post hook to handle updates in a post that are done via the admin edit post page.
But since I have more than ten custom post types, the save_post associated to each one of them is firing when I save ANY of all the posts.
I want to add an early return to the function that will return if the save post callback does not correspond to the current editing screen.
As follows:
function save_post_type_WHATEVER1{
if(not editing whatever1 cpt){
return;
}
}
function save_post_type_WHATEVER2{
if(not editing whatever2 cpt){
return;
}
}
But I don’t know what I could possibly check in the if(not editing whatever2 cpt){ to achieve the desired behaviour.
I see that the URL contains a ?post=x parameter. Would it be valid enough if I just do:
function save_post_type_WHATEVER2{
if(get_post_type($_GET['post'])!='whatever2'){
return;
}
}
Or will that turn into another kind of problem?
Thank you.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Any function hooked to the save_post hook will run whenever you save a post. That is expected behaviour.
You should use the hook save_post_{$post->post_type} to specify to which post type that callback is asociated. That way, it will only be called when saving a post of type {$post->post_type}
See the hook documentation
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0