I have serialized post meta. And i want to update single meta key. It looks like this:

enter image description here

In array:

$kisiArray = array(
   'option1' => 'foo',
   'option2' => 'bar',
   'option3' => 'apple',
   'option4' => 'orange'

I want to update only 'option3' to 'peach' so i am using update_post_meta function.

update_post_meta( $post_id, 'themeOps', ??? );

But i don't know what i must put the meta_value section. When i pust peach, it changes all meta keys to peach.

1 Answer 1


You must read custom field themeOps first, update index option3 in array and then save/update whole array.

$kisiArray = get_post_meta( $post_id, 'themeOps' );
$kisiArray['option3'] = 'peach';
update_post_meta( $post_id, 'themeOps', $kisiArray );

Update (sort by custom field)

$args = array(
    'post_type'  => 'post',
    'meta_key'   => 'themeOps_option3',
    'orderby'    => 'meta_value_num',
    'order'      => 'ASC',
$query = new WP_Query( $args );

More examples you find in Codex

  • Thank you it works. Last question, do you know how to use $kisiArray['option3'] in orderby meta_value_num? (I will set integer)
    – wpdev
    Commented Aug 10, 2018 at 15:21
  • @bilimokur You can't use custom field with serialized array as value to sort posts. Move $kisiArray['option3'] to separate custom field (e.g. themeOps_option3). When you will have the 'option3' value in a separate meta key, then you can use this key to sort posts as described here
    – nmr
    Commented Aug 10, 2018 at 15:52

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.