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?
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'
?