6

publish_post Runs when a post is published, or if it is edited and its status is "published". Action function arguments: post ID.

-Plugin API Documentation

I've added the publish_post hook to a WordPress plugin that I'm writing. The function called by the hook itself, is meant to change the categories of several posts using the wp_update_post function.

This hook does not work however as the result returned from running wp_update_post is always 0. My best guess is that running wp_update_post causes another instance of my hook to run because it re-publishes the post...which I believe brings about the "...or if it is edited and its status is "published"" of the statement above.

Is there any other action-hook that I can use that will only be called when the post added is completely new and not edited?

<?php
 /* 
 Plugin Name: Category Switcher Plugin
 Plugin URI: http://www.example.com
 Description: When a new post is created this plugin will cause the 
 Version: 0.1
 Author: Me
 License: GPL2 
?>
<?php
class categoryShifter {
  function shiftCategories($post_ID) {

    $maxNumPostsFirstTeir = 4;

    $first_teir_cat = "Fresh News Stories 1";
    $second_teir_cat = "Slightly Dated Stories 2";  

    $firephp = FirePHP::getInstance(true);

    $firephp->info('BEGIN: categoryShifter.shiftCategories()');

    $firephp->log($post_ID, 'post_ID: ');
    $firephp->trace('trace to here');    

    $first_teir_id = categoryShifter::getIDForCategory($first_teir_cat, $firephp); 
    $second_teir_id = categoryShifter::getIDForCategory($second_teir_cat, $firephp);

    $firephp->log($first_teir_id, '$first_teir_id');
    $firephp->log($second_teir_id, '$second_teir_id');   

    $qPostArgs = array(
      'numberposts' => 100,
      'order' => 'DESC', 
      'orderby' => 'post_date',
      'post_type' => 'post',
      'post_status' => 'published', 
      'category_name' => $first_teir_cat
    );

    $firstTeirPosts = get_posts($qPostArgs);   
    $firephp->log($firstTeirPosts, 'got posts:');

    $firephp->log(sizeof($firstTeirPosts), 'sizeof');


    // NOTE: This appears to work.
    for($i = sizeof($firstTeirPosts)-1; $i > $maxNumPostsFirstTeir-4; $i--) 
    {
      $newCats = array($second_teir_id);
      $editingId = $firstTeirPosts->ID;
      $result = wp_set_post_categories($editingId, $newCats); /* NOTE: Doesn't work presently... returns an array with the $second_teir_id in it. */
      $firephp->log($result, 'Result'); 
    }



    /*
    $my_post = array();
    $my_post['ID'] = 132;
    $my_post['post_category'] = array($second_teir_id);


    $firephp->log('Before', 'Before'); 
    if(wp_update_post( $my_post ) == 0) {
        $firephp->Error('Fatal Error, Post not updated', 'error');
    }
    $firephp->log('After', 'After');
    */
    return $post_ID;
  }


  function getIDForCategory($cat_name, $logger) {
    $logger->Info("Begin: getIDForCategory()");

    $cats = get_categories();      

    $whichCatId = "";

    foreach($cats as $single_cat) {
      if($single_cat->name == $cat_name) {
       $whichCatId = $single_cat->term_id;
       break;
      }
    }
    $logger->Info("End: getIDForCategory()");
    return (int)$whichCatId;
  }
}

/* Hook Post Creation */
/* add_action('publish_post', array('categoryShifter','shiftCategories')); */
add_action('wp_insert_post', array('categoryShifter', 'shiftCategories'));
?>

I've switched to using the wp_insert_post hook for the time being...but I still can't get the wp_set_post_categories function to change the categories of the posts.

I understand that I will probably need to update this code so that it takes into account the existing categories of the post and only modifies the ones specified by the plugin, but for now it's really just an alpha.

  • 1
    @leeand00 - Can you post your code as an update to your answer so we can be better able to see what needs correcting? – MikeSchinkel Feb 1 '11 at 5:49
  • @Mike Here's the update! – leeand00 Feb 1 '11 at 6:12
  • @MikeSchinkel Sometimes that just because I haven't gotten the chance to try the answers out yet, and I didn't want to mark one that's not a good answer....but I'll go back and see what I can do! – leeand00 Feb 1 '11 at 16:28
  • @MikeSchinkel Okay I cleaned them up and selected the best answers, sorry about that, I didn't realise how I had neglected this particular StackExchange. – leeand00 Feb 1 '11 at 16:34
17
add_action('new_to_publish', 'your_function');
add_action('draft_to_publish', 'your_function');
add_action('pending_to_publish', 'your_function');
5

Precisely target creation of new post is actually more tricky than it seems. Technically there are multiple ways post can get created or updated and there are plenty not so obvious things that are technically posts as well (revisions for example).

WordPress provides dynamic hooks that track not only post creation, but what it was and what it became. See Post Status Transitions in Codex.

1

More using experimenting than following the docs, this works for me (WP 3.3). I get a transition_post_status hook call with $new_status set to "auto-draft" when you create a new post.

function my_post_new($new_status, $old_status=null, $post=null){
    if ($new_status == "auto-draft"){
        // do stuff here
    }
}
add_action('transition_post_status', 'my_post_new');
  • 1
    This will fire off every time an auto-draft is saved, i.e. every minute or so when editing. – soulseekah Mar 23 '12 at 9:54
  • Good point. In my case this is not an issue but it could be for others, depending on what you were doing. – PapaFreud Mar 23 '12 at 10:50
  • i wanted to upvote, but i can't yet. so props go here, @PapaFreud . btw, this is now documented at codex.wordpress.org/Post_Status_Transitions . again, thanks man – Ana Ban May 22 '12 at 12:30
  • 1
    @soulseekah Interesting point you raised, I tested this, and auto-draft is assigned as the $new_status of a post upon its creation (i.e. Add New), once only. As soon as the first AUTOSAVE procedure fires after 1 minute, the $new_status value is updated to draft. The $old_status is still NULL until the post is manually saved or published. So technically this would work and not fire each time an the editor auto-saves your work. As an additional measure you could check to ensure that the $old_status is NULL to verify -> if ($new_status == "auto-draft" && $old_status === NULL) – userabuser May 22 '13 at 13:57
0

I have extensively read the WordPresss core and I have tried it all. wp_transition_post_status(), new_to_publish(), new_{post_type}(), wp_insert_post().

All of these are, after all, unreliable.

wp_transition_post_status() isn't reliable because the new status "publish" is the default status both for creating new posts and updating existing ones. Old status isn't reliable to define what's a new post and what's not because it can be draft, auto-draft, publish, etc.

new_to_publish() doesn't work for custom post types.

new_{post_type} only passes $post as parameter, and you can't know if it's new or updating existing one

wp_insert_post() have a $update parameter that should be TRUE if updating existing posts and FALSE if creating new posts, but it's unreliable because it returns TRUE for new posts because of auto-draft.

I ended up doing this:

/**
*   Do something when a new book is created
*/
function new_book($post_id, $post, $update) {
    if ($post->post_type == 'book' && $post->post_status == 'publish' && empty(get_post_meta( $post_id, 'check_if_run_once' ))) {
        # New Post

        # Do something here...

        # And update the meta so it won't run again
        update_post_meta( $post_id, 'check_if_run_once', true );
    }
}
add_action( 'wp_insert_post', 'new_book', 10, 3 );

Optionally, if you need to update your existing posts with the "check_if_run_once" meta, so it don't run the code above for existing posts created before you added that function, you could do:

/**
*   This is a temporary function to update existing posts with the "check_if_run_once" post meta
*   Access your website logged in as admin and add ?debug to the URL.
*/
function temporary_function() {
    if (current_user_can('manage_options')) {
        $posts = get_posts(array(
            'post_type' => 'book'
        ));
        foreach($posts as $post) {
            update_post_meta($post->ID, 'check_if_run_once', true);
        }
    }
}
if (isset($_GET['debug']));
    add_action('init', 'temporary_function');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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