• I did a search several times and several ways for this without luck. I’ve actually been looking and digging for a solution for a couple of weeks. I’m hoping it has not been brought up before.

    The reason that I’m posting here is because I’m having a problem with a site, but this is not about that specific problem, I’ll deal with that, this is about the need to improve the efficiency of adding and updating meta values in WP.

    When using add_post_meta() or update_post_meta(), each call to these functions can generate several db queries. On a highly customized site with many custom fields that need to be dealt with, publishing or updating a post can result in a timeout due to the number of db queries performed.

    I’ve dug around in the core code looking for some way to hook into the process and build a way to resolve the issue without luck. The main problem is that it appears that wpdb is incapable of doing multiple inserts or updates in a single query, if it can I can’t find it. The second problem I’ve run into is that there does not seem to be a way to prevent all the queries that are happening and build my own custom code to accomplish what needs to be done.

    This was my initial thought, cut down to the most essential steps:

    1. Register a shut down function to perform collected queries (see #4)
    2. Intercept all calls to update_post_meta() and add_post_meta() and store data to be updated or added into a queue
    3. Intercept all calls to get_post_meta() and check the queue for the value before passing control back to WP
    4. On shut down perform all inserts and updates using a single query for each

    Like I said, that was my initial idea, but I have been unable to figure out how to accomplish it with the hooks currently available in WP.

    What I would really like to see is a single function in WP that can be used to update or add multiple values at the same time. If get_post_meta() is called without a meta_key value then all meta values are retrieved in a single db query. It would be nice to also have a way to update or add multiple values with a single query. This would probably require new functions and improvement of wpdb so that it can do multiple queries at the same time. These functions could then be used by plugin authors of the popular custom fields plugins to improve the efficiency of their plugins.

  • The topic ‘Improve Efficiency of post meta inserting and updating’ is closed to new replies.