Home > Net >  Extract sub_array value from wordpress array in the database and save it back
Extract sub_array value from wordpress array in the database and save it back

Time:05-28

I have data stored in a custom field in array as such

a:5:{i:0;a:1:{s:12:"product_desc";s:44:"Value1";}i:1;a:1:{s:12:"product_desc";s:24:"Value2";}i:2;a:1:{s:12:"product_desc";s:31:"Value3";}i:3;a:1:{s:12:"product_desc";s:41:"Value4";}i:4;a:1:{s:12:"product_desc";s:39:"Value5";}}

I want to extract the values of the sub_array product_desc and update the custom field meta_value.

I know wordpress have maybe_unserialize to unserialize the data but what to do next to extract the sub_array values?

add_action( 'init', function() {
    if ( 'migrate' !== filter_input( INPUT_GET, 'action' ) ) {
        return;
    }
 
    $query = new WP_Query( [
        'posts_per_page' => -1,
        'post_type'      => 'post',
        'post_status'    => 'any',
    ] );
    if ( ! $query->have_posts() ) {
        return;
    }
 
    while ( $query->have_posts() ) {
        $query->the_post();
        
        $field_id_1 = 'podu';
        $field_value_1 = get_post_meta( get_the_ID(), $field_id_1, true );
        $data = maybe_unserialize( $field_value_1 );

      //How to extract sub_array values from $data?
    }
} );

CodePudding user response:

your $data contains an array, so you can loop over it:

foreach($data as $singleData){
    $desc = $singleDate['product_desc'];
    ... now you have your $desc
}

CodePudding user response:

WordPress's functions for handling metadata make this easy. But you'd never know that from their documentation.

This line gets your data. The get_post_meta() function includes maybe_unserialize(), so you get back your data structure with no extra work on your part.

$data = get_post_meta( get_the_ID(), 'podu', true );

This gives you back, in $data, the array of arrays you stashed in that metadata row.

array (
   array ( 'product_desc' => 'Value1' ),
   array ( 'product_desc' => 'Value2' ),
   array ( 'product_desc' => 'Value3' ),
)

Next you manipulate that $data as you wish. Maybe you want to create another array like this? It's hard to tell from your question.

array( 'Value1', 'Value2', 'Value3' )

To do that requires code like this. It's written defensively in case the contents of that metadata row aren't what you assumed they were.

$result = array();
if ( is_array( $data ) {
  foreach ( $data as $item ) {
    if( is_array( $item ) && array_key_exists( $key, $item ) {
      $result[] = $item['product_desc'];
    }
  }
}

Finally, you update the $result back into the metadata row.

update_post_meta( get_the_ID(), 'podu', $result, $data);

All the serialization happens behind the scenes.

Beware Short metadata keys can get you in trouble. All WordPress code, core code, plugin code, and theme code, share the same namespace for metadata keys. If you pick a short key and some other code picked the same short key, confusion will result. A good practice is to prefix your keys with your plugin or theme name. In other words use 'my_excellent_theme_podu' for your name, not just 'podu'.

  • Related