1

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.

4

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) – bilimokur Aug 10 '18 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 Aug 10 '18 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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