0

I am saving an array in post meta using update_post_meta.

$arr = array('this', 'or that');
update_post_meta($post_id, 'field', $arr);
$ret_arr = get_post_meta($post_id, 'field', true);
var_dump($ret_arr);

// string (13) "this or that"

Data as saved in the DB, copied and pasted from _postmeta table

a:2:{i:0;s:5:"this";i:1;s:7:"or that";

I then tried to do a wordpress query using the following

$res = $wpdb->get_results( "SELECT meta_value FROM wp_postmeta WHERE meta_key = 'field' AND post_id = '$post_id'" );
var_dump($res);

// array(1) { [0]=> object(stdClass)#673 (1) { ["meta_value"]=> string(13) "this or that" } } 

I have echo out my SQL Statement and used it in PHPMyAdmin and verified the returned data of the statement is serialized.

Finally I tried to build my own connection

$connect = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
    or die('Database Connection Failed. Error 1'); 
$db = @mysql_select_db(DB_NAME, $connect)
    or die('Database Connection Failed. Error 2');
$res = $wpdb->get_results( "SELECT meta_value FROM brated_postmeta WHERE meta_key = '".$field['id']."' AND post_id = '$post_id'", OBJECT );
var_dump($res);

// array(1) { [0]=> object(stdClass)#673 (1) { ["meta_value"]=> string(13) "this or that" } }

As you can see the results of all 3 data fetches return just the string "this or that" and is missing the serialized syntax and not even returning as an array with get_post_meta().

Can anyone explain what is going on and why I'm getting a simple string (or string missing serialized syntax) returned from my queries and and fetches and not a serialized array string or an array?

  • I can't reproduce any of your results, all examples you've provided work as expected for me. Note that only the post meta functions will unserialize the data for you, you won't get an array back from wpdb for that data. – Milo May 12 '16 at 5:13
  • I know this is very strange, but I wouldnt be here if it wasnt a factual issue. Im curious if I could have some strange setting in my DB that I should look at for this table and field in concern. – stoi2m1 May 12 '16 at 5:29
  • 1
    serialization is entirely a PHP construct, as far as MySQL is concerned, they're all strings with no special meaning. – Milo May 12 '16 at 5:33
  • @Milo thought this as well, thanks for reassuring me, i think i might have an issue else where in the logic of my function, im looking into this now. – stoi2m1 May 12 '16 at 7:02
  • @milo thanks for the feedback, this lead me back to my code which was solid, i commented it out just to see if something else was messing with my post_meta and found a plugin was still saving my post_meta. Im not sure the order but, either the plugin or my custom code was over writting eachother and i was getting odd results because of this. – stoi2m1 May 12 '16 at 17:05
0

In the end it was a plugin and my code conflicting with each other. Both were saving my post_meta. Resulting in odd behavior of my code and functionality of retrieving the data.

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.