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:

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.

2 Answers
2

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.

Leave a Comment