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'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?

share|improve this question
add comment

3 Answers 3

up vote 7 down vote accepted

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.

share|improve this answer
    
save_post is for when it's created or updated. You will want publish_post for when it's actually posted. If you don't do that - then drafts and such will be impacted as well. –  Sterling Hamilton Feb 10 '12 at 18:51
    
save_post is fired whenever wp_insert_post is called. On everything -- drafts, whatever. If you want to stop it from being called during auto drafts, you can check if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return somewhere in the function hooked into save_post. –  chrisguitarguy Feb 10 '12 at 20:08
    
+1 for edit_post function –  Warface Jul 2 at 13:28
add comment

the edit_post hook is probably the best hook... as it fires whenever a post/page is published or updated. save_post is another viable option... maybe better if you don't need it to fire when comments are updated.

http://codex.wordpress.org/Plugin_API/Action_Reference

the save_post page in the codex has an example of a function that fires whenever a post is saved:

http://codex.wordpress.org/Plugin_API/Action_Reference/save_post

share|improve this answer
add comment

There are several actions you can use. For example:

  • save_post
  • edit_post
  • publish_post
  • etc.

While lesser-used, there are also post-status transition hooks.

share|improve this answer
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.