I want to add custom field 'tnid' to all my posts, and have it's meta value taken from field 'name'

i have the following working query:

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)(
SELECT post_id, 'tnid', meta_value 
FROM wp_postmeta 
WHERE meta_key='name')

Now to my question. I want to add the new custom field only to post_type 'post', not to any other post_type. How do i have to edit the SQL query to accomplish that? thx

  • And @Philip, you need to do that via MySQL. Why not the WordPress way?
    – prosti
    Commented Jan 5, 2017 at 17:52

1 Answer 1


Hope this below SQL query will solve your problem-

INSERT INTO wp_postmeta 
(SELECT post_id, 
 FROM   wp_postmeta 
 WHERE  meta_key = 'name' 
        AND post_id IN (SELECT id 
                        FROM   wp_posts 
                        WHERE  post_type LIKE 'post'))

Please try it first. I haven't got the chance to try it.

  • Adding this way is non-WordPress way by the way.
    – prosti
    Commented Jan 5, 2017 at 18:43
  • He wanted this kinda SQL solution. So I tried to help him like that. I don't think this is a problem., right? @prosti
    – CodeMascot
    Commented Jan 5, 2017 at 18:45
  • we don't know what he needs until reply.
    – prosti
    Commented Jan 5, 2017 at 18:53
  • No man. He questioned in SQL where you proposed or commented why he is not trying the core WordPress methods and functions. So initially he needs SQL based solution. And beside he may have to run this query or code only once. So in that case I think the SQL method is best. So we better not argue on that and there could be a number of solutions. If you propose any WordPress based solution feel free to add it like an answer. Hope I made myself clear. @prosti
    – CodeMascot
    Commented Jan 5, 2017 at 18:57
  • I haven't found his feedback my quest. Maybe he replied and later removed the feedback in that case you are right.
    – prosti
    Commented Jan 5, 2017 at 19:07

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.