Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to limit publish_post to just original publication? WordPress


On a multisite running 3.0.1, the front of the site is suppose to show the most recent post with the category of "featured". The problem is this is handled like this:

add_action('publish_post', 'get_featured_post');

This apparently is also being triggered when the posts are updated. The code in get_featured_post should only run when the post is originally published, not updated.

I'm thinking I can lock this down by wrapping the code in get_featured_post() in an if statement like this:

if ($thisPost->post_status != 'publish') {
// most of the function code goes here

Are there any other post_status that I need to be wary of? Or does this cover it? Updating a draft won't call publish_post, right?

Answers (1)


Pippin Williamson answers:

use all three of these:

add_action('draft_to_publish', 'get_featured_post');
add_action('pending_to_publish', 'get_featured_post');
add_action('new_to_publish', 'get_featured_post');

Now it will only run the first time you publish.

Lawrence Krubner comments:

I'm dealing with a highly custom site that was originally built with Lyceum and then later upgraded to version 3. Can you suggest how I might be sure that action hooks like new_to_publish are present?

Pippin Williamson comments:

If "publish_post" works, then I am sure that the others will work. The best way to test for sure would be to set up a simple function that will test it.

function test_publish_actions() {
echo 'success'; exit;
add_action('draft_to_publish', 'test_publish_actions');
add_action('pending_to_publish', 'test_publish_actions');
add_action('new_to_publish', 'test_publish_actions');

If your screen goes white and says "success" when you publish, then it works.