Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
Does the hook get called at all? Also, you should use the prepare method to sanitize your SQL query. –  Stephen Harris May 1 '12 at 11:42
    
yes stephan, the hook get called when in "edit post" page but not in "quick edit" mode. however, i found out that i need to use $_GET[ID] in order to retrieve the post id in quick edit mode instead of the global $post. so now i use $my_id = get_post($_GET['ID']); is_array($my_id) ? $post_id = $my_id[ID] : $post_id = $post->ID; in my code to cover both cases of "edit post" or "quick edit". which works fine, but not when i am bulk editing multiple posts. any idea? thank you for the "prepare" suggestion. will definitely add it. –  Eyal May 1 '12 at 14:24
add comment

1 Answer

up vote 0 down vote accepted

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;

Remember also to sanitize input and prefix function names.

function wpse50651_filter_transition_post_status( $new_status, $old_status, $post ) { 

    global $wpdb;

    $wpdb->query( 
        $wpdb->prepare( 
          "UPDATE my_table SET post_status=%s WHERE post_id=%d", 
           $new_status,$post->ID
         ) 
     );
}
add_action('transition_post_status', 'wpse50651_filter_transition_post_status', 10, 3);
share|improve this answer
    
yeap. i do not know if it is good or bad but there is always a better solution than my solution. that works perfect and for bulk edit as well. thank you so much Stephen. and yes, i was just done reading from the link you posted about sanitizing SQL queries. everyday we learn something new. Tnx again. –  Eyal May 1 '12 at 19:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.