Perform an action when post is updated/published

I’d like to run a custom query using some meta data whenever a post is updated or published.

Is there something I can put in functions.php to fire when those events occur?

Related posts

Leave a Reply

4 comments

  1. The save_post action fires When a post is updated and/or published — including when a new post is inserted.

    <?php
    add_action( 'save_post', 'wpse41912_save_post' );
    function wpse41912_save_post()
    {
        // do stuff
    }
    

    If you want your functions to fire only when a post is being edited, you can hook into edit_post.

    If you want it to fire when a post is moved from draft to publish you can hook into transition_post_status.

  2. If you want to perform an action when any custom post update/save –

    add_action('save_post','save_post_callback');
    function save_post_callback($post_id){
    global $post; 
    if ($post->post_type != 'MY_CUSTOM_POST_TYPE_NAME'){
        return;
    }
    //if you get here then it's your post type so do your things....
    }