1

In the wp_postmeta table, I am seeing data in a field called "meta_value". The structure of the data is confusing. I am trying to understand what these values mean:

The meta_key field has a value of "_employer_socials"

a:4:{i:0;a:2:{s:7:"network";s:8:"facebook";s:3:"url";s:1:"#";}i:1;a:2:{s:7:"network";s:7:"twitter";s:3:"url";s:1:"#";}i:2;a:2:{s:7:"network";s:8:"linkedin";s:3:"url";s:1:"#";}i:3;a:2:{s:7:"network";s:9:"instagram";s:3:"url";s:1:"#";}}

Specifically, what does a:4: mean, or s:8, or i:1;a:2:

I see facebook and twitter, and can infer these are related to a companies social media handles, but the structure/format is confusing. There must be some way to decipher this?

My larger goal is to insert data into this field from an external source, but if I can't make sense of it, how do I do it?

1

1 Answer 1

1

WordPress will serialize arrays when saving to post meta. The data stored under the key _employer_socials in your example looks like this:

Array
(
    [0] => Array
        (
                [network] => facebook
                [url] => #
        )

    [1] => Array
        (
                [network] => twitter
                [url] => #
        )

    [2] => Array
        (
                [network] => linkedin
                [url] => #
        )

    [3] => Array
        (
                [network] => instagram
                [url] => #
        )
)

When you get the meta, WP will unserialize it for you, and it will look like above:

$employer_socials = get_post_meta( get_the_ID(), '_employer_socials', true );

You can then just work with the data as a regular array. When saving the array, WP will serialize it for you. Here are some basic functions that you can use as a starting point:

/**
 * Updates an existing network, or adds a new one if it does not exist.
 *
 * @param int    $post_id The Post ID to be updated.
 * @param string $network The name of the network to check.
 *
 * @return bool True if an update was made, false otherwise.
 */
function wpse_update_employee_social( $post_id, $network, $url ) {

    $employer_socials = get_post_meta( $post_id, '_employer_socials', true );

    // Update.
    if ( wpse_has_employer_social( $post_id, $network ) ) {
        foreach ( $employer_socials as $key => $employer_social ) {
            if ( $employer_social['network'] === $network ) {
                $employer_socials[ $key ]['url'] = $url;
                update_post_meta( $post_id, '_employer_socials', $employer_socials );
                return true;
            }
        }
    } else {
        // Add.
        $employer_socials[] = [
            'network' => $network,
            'url'     => $url,
        ];
        update_post_meta( $post_id, '_employer_socials', $employer_socials );
        return true;
    }

    // Nothing was updated.
    return false;
}

/**
 * Checks if a given network exists.
 *
 * @param int    $post_id The Post ID to be updated.
 * @param string $network The name of the network to check.
 *
 * @return bool True if the network exits, false otherwise.
 */
function wpse_has_employer_social( $post_id, $network ) {
    $employer_socials = get_post_meta( $post_id, '_employer_socials', true );
    foreach ( $employer_socials as $employer_social ) {
        if ( $employer_social['network'] === $network ) {
            return true;
        }
    }

    return false;
}

Example of adding a new employee social:

wpse_update_employee_social( 5302, 'wpse', 'https://wordpress.stackexchange.com/users/208214/user1669296' );

Example of updating an employee social:

wpse_update_employee_social( 5302, 'facebook', '#1234' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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