i am currently working on a wordpress plugin. the plugin includes a database table that being update every time a post is being created, edited or deleted with the data of that post. one of the columns in this table is “post_status” and i need it to be updated with the status of a post whenever it changes. right now i am using this code:
function filter_transition_post_status( $new_status, $old_status, $post ) {
global $post;
global $wpdb;
$wpdb->query(" UPDATE my_table SET post_status='$new_status' WHERE post_id=$post->ID");
}
add_action('transition_post_status', 'filter_transition_post_status', 10, 3);
the code above work fine when i change the post status within the “edit post” page. when i change the status of a post the change happens in my table as well. however, the code doesn’t work when i use the “quick edit” mode to change the status of post or bulk change multiple posts. the change does not happen in my table.
any help resolving this issue will be much appreciated.
thank you
I found the solution. When using the “quick edit” mode to update a post the post id cannot be retrieve using the global $post like in “edit post” page but by using $_GET[‘ID’]. So in order to cover both options, “quick edit” and “edit post” I am using the function below:
The function checks if $my_id get anything from $_GET[ID](in “quick edit” page) and if so it is going to use it otherwise it is going to use the global $post to get the id.
UPDATE:
I got a much better solution than to Stephen Harris – “You do not want to reference the global $post, but the post that is given to you as one of the argument. You simply need to remove global $post”