Update product price if specific meta_key contains a specific meta_value
Clash Royale CLAN TAG#URR8PPP
Update product price if specific meta_key contains a specific meta_value
In Wordpress / Woocommerce database there is a wp_postmeta
table.
wp_postmeta
I am try to update the meta_value = '0'
where meta_key = '_price'
only if another meta_key = 'group'
and meta_value = 'glass'
exist for the same post_id
.
meta_value = '0'
meta_key = '_price'
meta_key = 'group'
meta_value = 'glass'
post_id
This is what I have tried:
UPDATE wp_postmeta SET meta_value = '0'
WHERE meta_key = '_price' AND meta_key = 'group' AND meta_value = 'glass';
And This one too:
UPDATE
`wp_postmeta`
SET
`meta_value` = '0'
WHERE
meta_key = '_price'
AND `meta_key` IN (
SELECT
`meta_value` = 'glass'
FROM
`wp_postmeta`
);
Those attempts doesn't work.
How can I update the meta_value = '0'
(for meta_key = '_price'
) only if the meta_key = 'group'
and meta_value = 'glass'
exist for the same post_id
?
meta_value = '0'
meta_key = '_price'
meta_key = 'group'
meta_value = 'glass'
post_id
Any help is appreciated.
whats the problem?
– useless'MJ
Mar 16 at 12:37
Can you give a bit of context about where the code you pasted comes from and where it is implemented. Are you trying to change a plugin or is that homegrown code? Which hooks are you relying on; What is the error that you have? Is it a SQL question or Woocommerce or a Wordpress question?
– amirouche
Mar 16 at 19:20
1 Answer
1
Try this:
UPDATE wp_postmeta as pm
INNER JOIN wp_postmeta as pm2 on pm.post_id = pm2.post_id
SET pm.meta_value = '0'
WHERE pm.meta_key = '_price'
AND pm2.meta_key = 'group'
AND pm2.meta_value = 'glass';
it should work
Thank you so much and have a nice day! Works like a charm)
– Orkhan Hasanli
Mar 16 at 12:54
@OrkhanHasanli Welcome … It's not easy for all skilled SQL fellows here to understand the specificities of Wordpress / Woocommerce, when they are not used to.
– LoicTheAztec
Mar 16 at 12:58
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
See: Why should I provide an MCVE for what seems to me to be a very simple SQL query?
– Strawberry
Mar 16 at 12:34