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' );