0

When updating the database, I use update_post_meta($employee->ID, '_email_address', $record->email); and update this result:

object(stdClass)[1295]
  public 'email' => string 'he@fhflondon.co.uk' (length=32)

How does one target an inner array for files such as leadership, profile?

object(stdClass)[1295]
 public 'biography' => string '' (length=0)
 public 'files' => 
    object(stdClass)[1294]
      public 'leadership' => null
      public 'profile' => null
      public 'blog' => null
      public 'thumbnail' => null
2

You can't, get_post_meta and its associated APIs all work on key/value pairs, you can update the value, but you can't update a sub-section of it. You have to retrieve the entire value, modify it, then put it back as a whole.

Additionally, values are always strings, so to make this work WordPress will PHP serialize your data, introducing a raft of security attack vectors ( object deserialisation attacks for example ). Your post meta value is also very difficult to search for.

Instead, avoid this and all the problems associated by using multiple key/value pairs instead of serialising data into a single key/value pair. Remember, you can have several post meta key/values with the same key, keys aren't unique!

And if you must store multidimensional data in a serialised format, use JSON instead. Don't rely on WP to flatten out your objects and arrays into strings

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.