Support » Fixing WordPress » Can't get get_post_meta to work in a plugin

Viewing 6 replies - 1 through 6 (of 6 total)
  • Should TRUE be in caps?

    Steven, it doesn’t seem to matter if I use TRUE, true, or ‘true’.

    Update: I tried debugging with this code:

    $meta = get_post_meta($post->ID);
    $custom_field_keys = get_post_custom_keys($post->ID);
    print_r($meta);
    print_r($custom_field_keys);

    It works fine when the user who’s submitting a new post via my form is logged in: print_r dumps a whole bunch of data both times.

    But if the user is not logged in, get_post_meta and get_post_custom_keys return completely blank arrays — even though the custom fields are being set by my form (I can check and see them there when I go to edit the submitted posts).

    Weird. Still no idea why this is not working.

    Well, I did some digging into the WordPress core to find out out get_post_meta works, and I saw some references to cacheing that were new with version 2.9.

    On a hunch, I turned off cacheing in W3 Total Cache. Sure enough, get_post_meta started working again for non-logged-in users.

    I turned cacheing on step by step and was able to isolate the problem to database cacheing. So you can enable the page cache, object cache, and minify — but not the database cache. If you do, it stops get_post_meta from working unless the user is logged in (since cacheing is disabled for logged-in users by default).

    Moral: database cacheing in W3 Total Cache breaks get_post_meta.

    The caching isn’t breaking the post_meta, it’s caching it.

    Moral is: you should be doing development on a development area and only caching on live environments where no direct changes should be made.

    Solution is capture data from form with post:

    For example:

    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);
        $author_submit = stripslashes($_POST['user-submitted-name']);
        $email     = stripslashes($_POST['user-submitted-email']);
        if ($old_status != 'pending' && $new_status == 'pending') {
          $emails=get_option('notificationemails');
          if(strlen($emails)) {
            $subject='['.get_option('blogname').'] "'.$post->post_title.'" text';
            $message="text.\n\n";
    		$message.="name : $author_submit \n";
            $message.="Email : $email \n";
    		$message.="Text  :\n\n {$post->post_content}\n\n";
            $message.="Check : ".get_option('siteurl')."/wp-admin/post.php?action=edit&post={$post->ID}\n\n\n";
            $message.="data";
            wp_mail( $emails, $subject, $message);
          }

    Note: This solution applies only to the part where the user sends the data using the form.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Can't get get_post_meta to work in a plugin’ is closed to new replies.