Can't get get_post_meta to work in a plugin
-
I am trying to use get_post_meta to retrieve some custom fields from posts within a plugin.
The plugin is wp_status_notifier, which I didn’t write, but I’m adding some code to it.
For some reason, get_post_meta works when the user accessing the post (creating a new one, actually) is logged in. But when the user is not logged in, I have a form that creates a new post and assigns it to a default “guest” user, while setting the custom fields.
I know the custom fields are being set, because they show up elsewhere. But get_post_meta doesn’t retrieve them unless the user is logged in. Strangely, get_the_category does work.
Any ideas? I am banging my head against this and can’t figure it out. Here is the relevant code.
// Hook for post status changes add_filter('transition_post_status', 'notify_status',10,3); function notify_status($new_status, $old_status, $post) { global $current_user; $contributor = get_userdata($post->post_author); $authorname = get_post_meta($post->ID, 'authorname', TRUE); if($authorname!="") { $author = $authorname; } else { $author = $contributor->display_name; } $authormail = get_post_meta($post->ID, 'authormail', TRUE); if($authormail!="") { $email = $authormail; } else { $email = $contributor->user_email; } if ($old_status != 'pending' && $new_status == 'pending') { $emails=get_option('notificationemails'); if(strlen($emails)) { $subject='['.get_option('blogname').'] "'.$post->post_title.'" pending review'; $message="A new post by $author is pending review.\n\n"; $message.="Author : $author <$email> (IP: {$_SERVER['REMOTE_ADDR']})\n"; $message.="Title : {$post->post_title}\n"; $category = get_the_category($post->ID); if(isset($category[0])) $message.="Category : {$category[0]->name}\n";; $message.="Content :\n\n {$post->post_content}\n\n"; $message.="Review it: ".get_option('siteurl')."/wp-admin/post.php?action=edit&post={$post->ID}\n\n\n"; $message.="Powered by: WP Status Notifier"; $message.="\nID: {$post->ID}"; wp_mail( $emails, $subject, $message); } }
- The topic ‘Can't get get_post_meta to work in a plugin’ is closed to new replies.