admin管理员组文章数量:1293512
I've made two custom classes:
- FooBar
- BarSchizzle
I'm making a quite custom calculation of a bunch of things, that I would like to save in an array in a post_meta field.
This is how I save it
$obj1 = new FooBar();
$obj2 = new BarSchizzle();
$obj3 = new FooBar();
$arr = [
'a_key' => $obj1,
'another_key' => $obj2,
'a_third_key' => $obj3,
];
update_post_meta( $post_ID, 'my_custom_field', $arr );
// I also tried this, with same result
// update_post_meta( $post_ID, 'my_custom_field', serialize( $arr ) );
This is how I retrieve the array
$initial_arr = get_post_meta( $post_ID, 'my_custom_field', true );
$arr = unserialize( $initial_arr ); // And this is where the error occurs
I'm getting the error: unserialize() [function.unserialize]: Error at offset ...
(you can read more about it here).
My theory is, that WordPress hasn't gotten to loading (requiring) my custom classes, when it get's (and unserializes).
I've required the classes by simply slapping them into the functions.php
-file, in the top, like this:
require_once( __DIR__ . '/classes/FooBar.php' );
require_once( __DIR__ . '/classes/BarSchizzle.php' );
... Not inside any hook or anything. It felt a bit sloppy/bad, but it has been working perfectly up until now.
So this leaves me with two questions:
- Do I need to put my require of my custom classes inside a hook, like
init
or something? - Are there another explanation why WordPress stumbles when it tries to unserialize that
$arr
, containing my custom objects?
I've made two custom classes:
- FooBar
- BarSchizzle
I'm making a quite custom calculation of a bunch of things, that I would like to save in an array in a post_meta field.
This is how I save it
$obj1 = new FooBar();
$obj2 = new BarSchizzle();
$obj3 = new FooBar();
$arr = [
'a_key' => $obj1,
'another_key' => $obj2,
'a_third_key' => $obj3,
];
update_post_meta( $post_ID, 'my_custom_field', $arr );
// I also tried this, with same result
// update_post_meta( $post_ID, 'my_custom_field', serialize( $arr ) );
This is how I retrieve the array
$initial_arr = get_post_meta( $post_ID, 'my_custom_field', true );
$arr = unserialize( $initial_arr ); // And this is where the error occurs
I'm getting the error: unserialize() [function.unserialize]: Error at offset ...
(you can read more about it here).
My theory is, that WordPress hasn't gotten to loading (requiring) my custom classes, when it get's (and unserializes).
I've required the classes by simply slapping them into the functions.php
-file, in the top, like this:
require_once( __DIR__ . '/classes/FooBar.php' );
require_once( __DIR__ . '/classes/BarSchizzle.php' );
... Not inside any hook or anything. It felt a bit sloppy/bad, but it has been working perfectly up until now.
So this leaves me with two questions:
- Do I need to put my require of my custom classes inside a hook, like
init
or something? - Are there another explanation why WordPress stumbles when it tries to unserialize that
$arr
, containing my custom objects?
1 Answer
Reset to default 0Arrays and objects get automatically serialized/unserialized because they are php data types being stored as strings by MySQL. If you save array with update_post_meta you don't need to unserialize the returning value get_post_meta, function will do it for you.
Easier to look at update_post_meta function's source code. It calls update_metadata function where you can see a call of this function:
maybe_serialize( $meta_value );
As you can see, if data is array or an object type - it will serialize this data.
function maybe_serialize( $data ) {
if ( is_array( $data ) || is_object( $data ) ) {
return serialize( $data );
}
From the other side, when you use get_post_meta function, this chain
is bigger, but at the end it calls maybe_unserialize function, which do unserialization if needed.
maybe_unserialize( string $data )
In your case you are trying unserialize unserializable string. serialize() function returns false and generates E_NOTICE if it can't unserialize a value.
本文标签: functionsHow to save custom made object in an array in a post meta field
版权声明:本文标题:functions - How to save custom made object in an array in a post meta field 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741581220a2386580.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
meta_query
, causes fatal errors if you do a search replace query that changes the length of a string, and are difficult to debug – Tom J Nowell ♦ Commented May 2, 2021 at 23:53