edhogan
Member
Posted 3 years ago #
I have a plugin with a that adds a save_post hook.
I notice that if I click on "save post", I might see my save_post hook invoked with post_id=247 (for example), then if i modify the post by adding images, and then click on "save post" again, now my save_post hook is invoked with a different post_id=252, and invoked with post_id=247.
How am I supposed which is the "real" post_id? Is there a way to determine that 252 was a synonym for 247?
edhogan
Member
Posted 3 years ago #
I appears that the post_id that save_post is being invoked with is some sort of "revision" number that is related to the autosave. Any idea how i can go from a revision number to a post_id?
edhogan
Member
Posted 3 years ago #
To close the loop...
here is a way to get to the correct post_id inside of save_post. Things like autosave and a normal click on save will bump the revision number of the post. Then save_post is called with that number. To go from the revision number back to the actual post_id, use the SQL query:
SELECT post_parent FROM " . $wpdb->posts . " WHERE ID = " . $post_id;
function my_action__save_post($post_id, $post) {
global $wpdb;
/* autosave and other things appear to bump the post_id */
$sql_get_real_post_id = "SELECT post_parent FROM " . $wpdb->posts . " WHERE ID = " . $post_id;
$post_id_answer = $wpdb->get_results($sql_get_real_post_id);
if ($post_id_answer[0]->post_parent != 0) {
$post_id = $post_id_answer[0]->post_parent;
}
/* now $post_id is really correct */
...
Seems to be working great! Thanks for sharing the solution.
danleroux
Member
Posted 2 years ago #
or you can do this:
$post_id = get_post($post_id)->post_parent;