2

I'm trying to get the title and content from the global $post variable when a user publishes a new post (to submit to an external web service, which in turn needs to pass back additional data to add to the post).

The hooks I tried using so far ('transition_post_status' and 'publish_post') all seem to run before the post is inserted into the database. This results in an empty string everytime my code tries to read data from $post, as (I assume?) this variable is fetched from the Wordpress DB, and - because a new post is applicable - nothing has yet been stored in the DB to begin with.

Is there a hook that fires after a post's status is transitioned to which I can hook a function? Alternatively, is it possible to somehow get the post title and content from the TinyMCE editor via PHP?

Would appreciate any help!

Example of what I'm currently sitting with:

add_action('transition_post_status','dostuff',10,2);

function dostuff( $new_status, $old_status ) {
    if ($new_status == 'publish' && $old_status != 'publish') {
        global $post;
        $content = $post-> post_title . "\n" . $post->post_content; //Title and content are empty :(
        //Do more stuff with $content
    }
}

2 Answers 2

3

The third parameter for transition_post_status is the post object. Use it.

add_action( 'transition_post_status', 'dostuff', 10, 3 );

function dostuff( $new_status, $old_status, $post ) {
}

When in doubt, do not use globals. They are not reliable, they make your code hard to test and to read.

3
  • 1
    Having this filter load on every post transition - especially if a call is being made to an external web service - may affect performance, even if it fails the if/else checks to facilitate completing the call. Oct 19, 2014 at 23:24
  • @toscho thanks, this works perfectly. I'm marking this as the correct answer. What would be the difference between declaring the $post variable in the function's parameters versus inside the function? (If inside the function, it would start working after the post was saved a first time, which led to my "not-yet-in-the-DB hypothesis.)
    – player.mdl
    Oct 20, 2014 at 4:05
  • @player.mdl See my edit. Using global is not an internal declaration, quite the opposite. And WP doesn’t push every post object into the global namespace.
    – fuxia
    Oct 20, 2014 at 6:02
0

I'm unable to comment yet, as I feel it may be a more suitable spot than to post an actual answer, in case your code above is a typo, but your line:

$content = $post-> post_title . "\n" . $post->post_content; //Title and content are empty :(

might be producing an error as there is a space inbetween $post-> post_title. It should be

`$post->post_title`

(no space after "->")

1
  • 1
    You can have any amount of spaces or line breaks around the ->. That’s valid PHP.
    – fuxia
    Oct 19, 2014 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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