im currently developing a plugin that will read an external xml, and convert the data to a custom post type. Each post needs around 50-60 meta_fields. Right now im looping through each piece of needed data in the xml and running a update_post_meta() on each of them. This turns out to be very inefficient because each update_post_meta results in a separate request, which in my case results in a server timeout when the xml has a lot of data.

imagine that the $event['meta'] has 50 fields, and I need to run all 50 updates on 500 posts in one go.

    // add/update meta fields
    foreach ($event['meta'] as $meta_key => $meta_value) {
        update_post_meta( $post_id, $meta_key, $meta_value );

Is it possible to combine all the meta updates into one request? maybe in combination with the wp_insert_post() ?

its important that it must create a meta field if it doesn't already exist, while updating it if it does, much like update_post_meta does.

  • Do you need all meta fields to be searchable (full text)?
    – kaiser
    Mar 13, 2015 at 16:28
  • What do you mean? Some fields needs to be searchable as string, most fields just need to be displayed on templates.
    – Malibur
    Mar 13, 2015 at 16:41
  • if meta data needs to be part of a query, then it needs to be in an individual row in the database, and must be inserted individually. if meta data does not need to be part of a query, it can be saved as an array under a single key, which will only require one insert.
    – Milo
    Mar 13, 2015 at 16:46
  • Yeah i will need to use a lot of it in filters for WP_Query objects, so I'd prefer if it got saved in separate meta_fields
    – Malibur
    Mar 13, 2015 at 16:48
  • would it even be possible with sql commands to handle all meta data for one post in one go??
    – Malibur
    Mar 13, 2015 at 16:48

1 Answer 1


Do all insert manually via wpdb (SQL). you can do multiple inserts with one sql statement. the wp_post_meta is very simple built.

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.