L
L
ligisayan2018-02-22 11:04:23
MySQL
ligisayan, 2018-02-22 11:04:23

How to set a default value for a new meta field in wordpress?

Hello! I have a wordpress site with a lot of ads (post type created by the theme). I added a new custom meta field cp_wholespare for ads , which has only 2 values ​​(Spare part|whole car), which is selected by a radio button when editing.
The problem is that in the body of the ad, although the first of the available default values ​​(spare part) is selected as active, it is not registered in the database and, accordingly, when filtering by this field, it does not search.
If you set the switch manually, then in the search by meta field in the table it finds values ​​according to the structure

SELECT * FROM `wp_postmeta`

meta_id  post_id  meta_key       meta_value
459584   20025    cp_wholespare  автомобиль целиком
459621   20018    cp_wholespare  Запчасть

Question: how to set the default value for this field for all ads at once?
I managed to select all ads like this:
SELECT *
FROM wp_posts 
INNER JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE 1=1
AND wp_posts.post_type = 'ad_listing'
AND wp_posts.post_status = 'publish'
AND wp_postmeta.meta_key = 'cp_category'

And how now to assign the meta-field 'cp_wholespare' to everyone with the value 'Spare part'? So?
UPDATE *
FROM wp_posts 
INNER JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE 1=1
AND wp_posts.post_type = 'ad_listing'
AND wp_posts.post_status = 'publish'
AND wp_postmeta.meta_key = 'cp_category'
ON wp_postmeta.meta_key = 'cp_wholespare'
SET wp_postmeta.meta_value = 'Запчасть'

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question