What format is this ?
-
I want to know what format is this and if I can create my own ?
a:1:{s:10:”subscriber”;b:1;}
I think it’s something like an array data ?
-
This topic was modified 18 hours, 10 minutes ago by
wildapache.
-
This topic was modified 18 hours, 10 minutes ago by
-
This is a serialized string. Implemented in PHP using
serialize(), it is used in WordPress, for example, for arrays that are entered in option fields. See also: https://developer.wordpress.org/reference/functions/add_option/Thanks !
I found this code in a meta value.
So, now I can create a meta key with a multiple values it will very helpful to short some code.
Strange.
I can serialize any type of arrays ? Or ?
I created an array, serialize it, after, without put the data in db unserialize it, and it works.
But, if a put serialize data in DB and after GET it data for unserialize it’s doesnt work.
Tomorrow i try to understand why
WordPress also serializes the content for you when it comes to meta fields. You don’t have to worry about it yourself. Just use
update_post_meta()andget_post_meta()orupdate_option()andget_option(), and don’t access the fields directly.I need to update user meta, and now I think is a wordpress function add or store serialized array in wrong way. Because if I insert it directly in DB it work, if I do via update_user_meta, it doesnt.
I found where was a problem.
WordPress serialize it in automatically.
So, I remove serialize, and put just array
I my case it was a double serialization π
-
This reply was modified 15 hours, 30 minutes ago by
wildapache.
That’s exactly what I’ve already explained to you several times above. It’s also in the manual for those functions. You don’t have to worry about it yourself. I’m glad it’s working now.
@threadi another related question.
Can I find exact data in serialized array ?
For example I have an array like this : $exp = array(“start” => “20:00”);
How can I get exact data from user > meta_value ?
First save it:
update_user_meta( 42, 'my_value', $exp );Then read it:
$exp = get_user_meta( 42, 'my_value', true );Output the “start” index after reading:
echo esc_html( $exp['start'] );Yes I do the same with my data.
But question is a little bit another.
For example, we have 10 users, they all have a in _usermeta, meta_value like $exp = array(βstartβ => β20:00β);
Now, I need to find 5 users which meta_value where start == 20:00.
I read some faq, how I can understand wordpress do not unserialize data when it make a query and we need make search like in a normal text ?
-
This reply was modified 2 hours, 32 minutes ago by
wildapache.
You can retrieve that kind of information using
WP_User_Query. Example:$query = array(
'meta_query' => array(
array(
'key' => 'my_value',
'value' => 's:5:"start";s:5:"20:00";',
'compare' => 'LIKE',
)
)
);
$users = new WP_User_Query( $query );See: https://developer.wordpress.org/reference/classes/wp_user_query/
Thanks !
Hi @wildapache,
WordPress does not unserialize the data while running a Database query. If you frequently need to search for or filter a value in the serialized data, storing it separately is a better design choice. So it can be queried directly.
As a workaround, you can unserialize the data after retrieving it from the database using PHP’s unserialize() function. Then search for the values in the unserialized data.
Can you please share an example of your serialized data with multiple records that match startΒ ==Β 20:00?
-
This reply was modified 15 hours, 30 minutes ago by
You must be logged in to reply to this topic.