Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Why am I having trouble accessing post meta data in a future_to_publish add action in functions.php?

I have tried:

function post_to_twitter() {
    global $post;
    $url = get_post_meta($post->ID, 'short_url', true);
    if(!isset($url) || $url == "") $url = get_permalink($post->ID);
    //post to Twitter code
}
add_action('future_to_publish', 'post_to_twitter', 5);

I have also tried:

function post_to_twitter($post) {
    $url = get_post_meta($post->ID, 'short_url', true);
    if(!isset($url) || $url == "") $url = get_permalink($post->ID);
    //post to Twitter code
}
add_action('future_to_publish', 'post_to_twitter', 5, 1);

The post to Twitter code is working just fine, and the function is being called when a scheduled post is published. The code above is trying to get a post meta value "short_url", but it doesn't seem to be working. I believe it has something to do with the $post variable. The exact same code is working perfectly for a "publish_post" function. Any ideas?

share|improve this question
    
Hi @epaps - Where does 'future_to_publish' come from? Where is the doaction('future_to_publish'); called? I don't find it in WordPress anywhere... –  MikeSchinkel Jan 30 '11 at 1:47
    
Why are you setting priority to 5? –  MikeSchinkel Jan 30 '11 at 2:42
    
No reason in particular -- just to make sure that it's called early. –  epaps Jan 30 '11 at 2:44

1 Answer 1

Hi @epaps:

So I tested your code and it all works fine. The $post is passed in as an object and you have full access to the $post->ID variable. Best I can tell is that you have some kind of plugin interaction that is causing it to fail or that, for some reason the scheduled task is not being called (but if the future post is indeed being published then the latter potential does not seem possible.)

Hmm, without your full code to debug I'm stumped. If you come up with me information maybe I can help you find it.

P.S. Here was my first answer which was clearly wrong: Could it be that you meant to use the 'publish_future_post' hook instead of 'future_to_publish'?

share|improve this answer
    
Mike, take a look at this: codex.wordpress.org/Post_Status_Transitions The function IS being called though, so I'm not sure if "future_to_publish" is the problem or not -- I'm thinking that it has more to do with what's inside the function. Have you seen "publish_future_post" used over "future_to_publish"? Do you think that either of those code snippets should work? –  epaps Jan 30 '11 at 1:51
    
@epaps - Ah, you are right. I searched the code for it and couldn't find it and that's because it's composed at runtime. Let me look at it closer... –  MikeSchinkel Jan 30 '11 at 2:04
    
I appreciate the help, Mike. –  epaps Jan 30 '11 at 2:05
    
I think that a combination of using "publish_future_post" and using $post_ID instead of $post->ID may have solved the problem. I'll let you know if it ends up working. –  epaps Jan 30 '11 at 3:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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