Skip to content
Advertisement

Custom field in multiple selection

I’ve started WordPress 1 month ago and i’ve just done my first hook into the website (just to add/remove an estimated delivery date).

Once i’ve had this I was thinking that I’ve broke the website (LMAO) but it was just a postmeta problem. The postmeta value just need to be added on on every single products and be turned to yes.

My problem is that I don’t want to do it manually (164 products will be long). Is there a way to do it with some SQL or better add the checkbox to the multi-selection menu in the All product page so everyone is editing the site can use it ? Thanks !

NOTE : I’m a newbie with WP but not with PHP and SQL

Advertisement

Answer

Not sure to understand what you need exactly… but here is a query that can helps you to insert meta value for all posts of a given post type :

insert into wp_postmeta (post_id, meta_key, meta_value) 
select ID,'your_meta_key', 'your_meta_value' from wp_posts where post_type="your post_type"

This will insert a meta_key/meta_value pair in wp_postmeta for every posts matching the post_type.

You can replace the meta_value by a value of your choice or a value from the current wp_posts row (for exemple you can replace 'your_meta_value' by post_title to add the current post_title in the postmeta).

To update existing meta_values of all posts for a given post_type :

update wp_postmeta set meta_value="your new value" where
meta_key = 'your_meta_key'
and post_id in (select ID from wp_posts where post_type="your post_type")
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement