WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Can't get get_post_meta to work in a plugin (7 posts)

  1. dtweney
    Member
    Posted 1 year ago #

    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);
    
          }
    	}
  2. Steven Jones
    Member
    Posted 1 year ago #

    Should TRUE be in caps?

  3. dtweney
    Member
    Posted 1 year ago #

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

  4. dtweney
    Member
    Posted 1 year ago #

    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.

  5. dtweney
    Member
    Posted 1 year ago #

    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.

  6. Steven Jones
    Member
    Posted 1 year ago #

    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.

  7. loorlab
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic