add_action on inherit post status

I’m trying to use the add_action() hook to run a custom function, but am struggling with the post status.

I originally tried using:

Read More
add_action('pending_to_publish_portfolio', 'my_function');

(portfolio being my custom post type).

This didn’t work, so i posted in the official WordPress Support forum for some help.
I was given the following code to get the right transition status’

function bj_test($new, $old) {
print_r($old . '_to_' . $new);
echo '<br>';
print_r($old . '_' . $new);
die();
}
add_action('transition_post_status', 'bj_test', 10, 2);

This gives me 2x status’:

new_to_inherit
new_inherit

I’ve looked at the inherit status and the codex simply states it’s used for post revisions. Which makes sense as i’m changing from a pending post to a published post.

My problem is, i just don’t know how to use this information now.
All i’m trying to do is trigger an event when i changed a post from pending to published.
According to all the online tutorials i can find it should be as easy as my very first attempt (posted above), but that just doesn’t work.
Has something changed recently in the way post transitions are handled so all these tutorials are out of date?

It’s probably really simple for somebody who knows what they’re doing, but i’m very new to WP and am running out of options 🙁

any help appreciated.

Related posts

Leave a Reply

2 comments

  1. I’m running into this same issue in some code I’m developing.

    In your case though tree is a better hook as you only want to hook posts that are moving to ‘publish’

    You need to hook your function into all of the possible {status}_to_publish hooks like this:

    add_action('new_to_publish', 'my_function');
    add_action('draft_to_publish', 'my_function');
    add_action('auto-draft_to_publish', 'my_function');
    add_action('pending_to_publish', 'my_function');
    add_action('future_to_publish', 'my_function');
    add_action('private_to_publish', 'my_function');
    

    You found even pass the initial statues is using a foreach loop using the first part of the hook names from an array.

  2. Your code is almost there. The bare-bones version would be:

    function bj_test($new, $old) {
      if ('pending_to_publish' == $old . '_to_' . $new) {
        // run your code
      }
    }
    add_action('transition_post_status', 'bj_test', 10, 2);
    

    That will run for any post type, though, and it looks like you only want it for one type. There is no pending_to_publish_{cpt} hook (that I know of) but there is a {$new_status}_{$post->post_type} hook, but that will run every time the post is saved, not just when the transition changes. Also not quite right.

    But…

    function run_cpt_action($a,$b) {
      // run your code
      var_dump($a,$b); die; // debugging only
    }
    
    function bj_test($new, $old) {
      if ('pending_to_publish' == $old . '_to_' . $new) {
        add_action('publish_book_type','run_cpt_action',1,2);
      }
    }
    add_action('transition_post_status', 'bj_test', 10, 2);
    

    … because of the order in which the filters fire, you can combine the two and get this working.