• Resolved wildapache

    (@wildapache)


    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 23 hours, 49 minutes ago by wildapache.
Viewing 14 replies - 1 through 14 (of 14 total)
  • Moderator threadi

    (@threadi)

    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/

    Thread Starter wildapache

    (@wildapache)

    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.

    Thread Starter wildapache

    (@wildapache)

    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

    Moderator threadi

    (@threadi)

    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() and get_post_meta() or update_option() and get_option(), and don’t access the fields directly.

    Thread Starter wildapache

    (@wildapache)

    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.

    Thread Starter wildapache

    (@wildapache)

    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 21 hours, 9 minutes ago by wildapache.
    Moderator threadi

    (@threadi)

    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.

    Thread Starter wildapache

    (@wildapache)

    @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 ?

    Moderator threadi

    (@threadi)

    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'] );
    Thread Starter wildapache

    (@wildapache)

    @threadi

    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 8 hours, 11 minutes ago by wildapache.
    Moderator threadi

    (@threadi)

    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/

    Thread Starter wildapache

    (@wildapache)

    @threadi

    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?

    Thread Starter wildapache

    (@wildapache)

    @mbigul

    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.

    I make it before, and it works well, but in this way I have a multiple not much grouped meta_key.

    After I noticed in usermeta this a:1:{s:10:”subscriber”;b:1;} and I asked what is it and how it works.

    Now is more clear, about how it works, but I am looking for a “standart” way to use it instead of create my own methods. WordPress read seriazile data like a normal text, ok, for this exist solutions too, which wrote about @threadi

Viewing 14 replies - 1 through 14 (of 14 total)

You must be logged in to reply to this topic.