Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7835723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:46:08+00:00 2026-06-02T13:46:08+00:00

I want to perform some updates to posts’ meta data in a WordPress site

  • 0

I want to perform some updates to posts’ meta data in a WordPress site (wp_postmeta table).
The known good’ol function update_post_meta() is doing the job and also will check and if key not exist, it will add it.
But, this function can handle only one post at a time.
I want to save expensive db calls and update with one query several fields for different post ids.

Generally, I could use mySQL UPDATE query with CASE to swich and assign each meta_value according to post_id, like in this example, and update some values of different rows in the table.
But, it may be that for some of the posts, that meta_key is not existing yet, so INSERT is needed and not update. Well, if not using the update_post_meta(), I should check this somehow.
I tried to use INSERT .... ON DUPLICATE UPDATE (syntax) and also REPLACE (syntax), but it is relevant only for columns that are unique or defined as primary keys, so it doesn’t help in this case.

Is there a way or an idea for how can I have the “Insert value, and update if already exists this meta_key for this post_id” and do this for several different post_ids (same meta_key however all the way), and with one single mySQL query?

Thanks
(* I asked this also in WordPress answers, and asking here too)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:46:10+00:00Added an answer on June 2, 2026 at 1:46 pm

    REPLACE and INSERT ... ON DUPLICATE KEY UPDATE are not “relevant only for columns that are unique or defined as primary keys”, rather the determination of whether the record is new or already existing (and thus needs to be replaced/updated) is made on whether there is a unique key collision or not. As the MySQL documentation states:

    MySQL uses the following algorithm for REPLACE (and LOAD DATA ... REPLACE):

    1. Try to insert the new row into the table

    2. While the insertion fails because a duplicate-key error occurs for a primary key or unique index:

      a. Delete from the table the conflicting row that has the duplicate key value

      b. Try again to insert the new row into the table

    It describes INSERT ... ON DUPLICATE KEY UPDATE similarly.

    Therefore, provided your REPLACE or INSERT ... ON DUPLICATE KEY UPDATE command identifies your updated records by their current primary keys, it should do exactly what you wish. According to the WordPress documentation, the primary key is the meta_id field, so with a SELECT you obtain this identifier for the existing cases and with an outer join, get NULL if it doesn’t already exist (which sets the column to the next auto_increment value):

    REPLACE INTO `wp_postmeta` (
          `meta_id`
        , `post_id`
        , `meta_key`
        , `meta_value`
    )
        SELECT
              `meta_id`
            , `post_id`
            , "key of interest"
            , "new value" -- assuming all posts to be set the same
        FROM
            `wp_postmeta`
            NATURAL RIGHT JOIN (
               SELECT 1 AS `post_id` -- where 1 is the first post_id
               UNION SELECT 2        -- 2 is the second
               UNION SELECT 3        -- 3 is the third
               -- etc.
            ) AS `temp_table`
        WHERE
            `meta_id` IS NULL OR `meta_key` = "key of interest"
    ;
    

    If the new meta value is to be different for each post, you could add such values as a second column in temp_table:

    REPLACE INTO `wp_postmeta` (
          `meta_id`
        , `post_id`
        , `meta_key`
        , `meta_value`
    )
        SELECT
              `meta_id`
            , `post_id`
            , "key of interest"
            , `new_value`
        FROM
            `wp_postmeta`
            NATURAL RIGHT JOIN (
               SELECT 1 AS `post_id`, "foo" AS `new_value`
               UNION SELECT 2, "bar"
               UNION SELECT 3, "qux"
               -- etc.
            ) AS `temp_table`
        WHERE
            `meta_id` IS NULL OR `meta_key` = "key of interest"
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table on which I want to perform some operations every hour.
What do I want to achieve: I want to perform some time consuming operations
I want to create a simple batch file that would perform some Visual Studio
We want to perform some calculations on some values in a view...so when the
I have a repeating AlarmManager that calls a Service to perform some background updates.
I want to save to a log file some SQL query rails performs, (namely
I want perform a easy task in android, which when receiving a call the
I want to perform rubyf action in VIM when I press F5 if the
I want to perform simple validation against multiple fields. Please note these fields are
I want to perform a printf() to display when the currently executing code was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.