2

I've been testing and trying to find where it possibly going wrong but i'm stuck on that part. The get_post_meta() is not returning any of my values, tested with a var_dump(get_post_meta(1)) this seems to return it, but when accessing that key var_dump(get_post_meta(1, 'key_name')) it's returning a bool(false).

Also the following way is not working for me, var_dump(get_post_meta(1, 'key_name', true)); as by default it's false so checked this also. In my database the key 100% exists and has got values, also the ID is correct sending this.

<?php 
    var_dump(get_post_meta(1));                // array(4)
    var_dump(get_post_meta(1,'key_name');      // array(1){[0]=>bool(false)}
    var_dump(get_post_meta(1,'key_name',true); // bool(false)
?>

Is there something wrong with WordPress on this or am I missing something here, i've got a huge storage of data, could this be a problem for WordPress? Using the latest WordPress version available by download of wordpress.org.


Edit: More Information Required

Thank you for you're comment. I'm simply using the add_post_meta() and update_post_meta(). Don't understand this, i'm working with this everyday also but have never seen this before happening to any of my coding. Inside the admin part it works without any problems. the code:

<?php
    // $pID is 526660
    // Checked this with a diffrent var_dump
    var_dump(get_post_meta($pID));
    var_dump(get_post_meta($pID, 'agenda_postmeta'));
    var_dump(get_post_meta($pID, 'agenda_postmeta', true)); 
?>

Returns

// First var_dump
array(4) {
  ["agenda_postmeta"]=>
  array(1) {
    [0]=>
    string(843) "(serialized data string, hide this for privacy reasons on my comment)"
  }
  ["agenda_date"]=>
  array(1) {
    [0]=>
    string(10) "2016-10-08"
  }
  ["agenda_location"]=>
  array(1) {
    [0]=>
    string(17) "Jaarbeurs Utrecht"
  }
  ["agenda_city"]=>
  array(1) {
    [0]=>
    string(7) "Utrecht"
  }
}

// Second var_dump
array(1) {
  [0]=>
  bool(false)
}

// Third var_dump
bool(false)

Edit: Extra Information add/update postmeta

Below is the code using to save this to my database, it's working because the database has it's keys/values.

Database values by post_id

// Check for update or add
if( $has_id != 0 )
{
    update_post_meta( $post_id, 'agenda_postmeta', $postmeta );
    update_post_meta( $post_id, 'agenda_date', date("Y-m-d", strtotime($_POST["party_date"])) );
    update_post_meta( $post_id, 'agenda_location', $_POST["party_location"] );
    update_post_meta( $post_id, 'agenda_city', $_POST["party_city"] );
}
else {
    // If no posted_id available insert post_meta
    add_post_meta( $post_id, 'agenda_postmeta', $postmeta );
    add_post_meta( $post_id, 'agenda_date', date("Y-m-d", strtotime($_POST["party_date"])) );
    add_post_meta( $post_id, 'agenda_location', $_POST["party_location"] );
    add_post_meta( $post_id, 'agenda_city', $_POST["party_city"] );
}
  • There's some things that can go wrong with this. First, the value 1 inside the get_post_meta arguments, is the post ID, then the 'key_name' is generally the id of some custom field. So, it seems like you have 4 custom fields, make sure all of them have some value for the post with an ID of 1. Also, if you are using some plugin to place your custom fields on the post editor, the values of those not always are retrieved with get_post_meta, sometimes the plugins have their own function to get the values. – Eduardo Sánchez Hidalgo Urías Sep 20 '16 at 8:34
  • Thank you for you're comment. It has 4 diffrent postmeta values that's correct, i'm calling the key with values, non of them are empty. But when accessing this data it returns a bool(false). The postmeta is customfield but should work, after the latest update it's not working anymore. Seems like a change inside the WordPress core functions. Could a query call help in this or is there something wrong with calling this function ? – Erik Kraijenoord Sep 20 '16 at 8:39
  • I don't know, I would need more information to form an answer. I work with WordPress every day, and no update has made my code crash lately and I use get_post_meta often. Can you share the code where the custom post fields are registered?? can you tell us where (what file) are you using the var_dump(get_post_meta(1)); – Eduardo Sánchez Hidalgo Urías Sep 20 '16 at 9:01
  • Changed my original post, this is the actual return and setup in my coding right now. – Erik Kraijenoord Sep 20 '16 at 9:11
  • Ok. Are you sure the metaboxes and custom fields are not placed on the post editor with a plugin like metabox.io or Advanced Custom FIelds. What I want to see is the code (probably in functions.php) where the custom fields and metaboxes are registered. Anyway if that does not work, you can just get the values from the first array: $cf_array = get_post_meta($pID); $agenda_postmeta = $cf_array["agenda_postmeta"][0]; – Eduardo Sánchez Hidalgo Urías Sep 20 '16 at 9:22
1

Apparently the serialized data got corrupt, calling the field by query got me this error. WordPress does not return an error when the serialized data is corrupt just a bool(false).

Solved this by insert/update the serialized data in a base64_encode() and call it with base64_decode(). This seems to do the trick and data can be called again from the function.


Wrote a small functions for myself

Maybe there is someone with the same issue, i've wrote this small function to check.

<?php
function wpgz_post_meta( $post_id, $meta_key="", $base64_decode=false )
{
    if( intval($post_id) )
    {
         $post_meta = get_post_meta( $post_id, $meta_key, true );
         $post_meta = $base64_decode !== false ? base64_decode($post_meta) : NULL;

         $post_meta = explode(PHP_EOL, $post_meta);
         $post_meta = unserialize($post_meta[0]);

         if( is_array($post_meta) )
         {
             return $post_meta; 
         }
         else {

             send_page_notice("danger", "Invalid format", "Returned value is not a valid array.");
         }
     }
     else {

         send_page_notice("danger", "Invalid input", "The first value for this function should be an integer number.");
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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