Insert meta_key if another meta_key exist - php

I trying to copy data from one product feed plugin to another. First thing I have to do is create a empty meta_key '_woosea_brand' but only to this posts where another meta_key 'woocommerce_ceneo_Producent' exist. Later i will copy the meta_value (already figure it out ). The problem is to insert the empty meta_key = '_woosea_brand" if meta_key = 'woocommerce_ceneo_Producent' exist.
I was trying this code: I found it in another stackoverflow post. But it change meta_value.
PhPmyadmin SQL Query to insert meta_key & value IF another value exist
INSERT INTO wp_postmeta (meta_key, meta_value, post_id)
SELECT "_wplp_post_front", 1, post_id
FROM wp_postmeta outside_table
WHERE meta_key = 'qty'
AND meta_value = '0'
AND NOT EXISTS
(SELECT * FROM wp_postmeta
WHERE post_id = outside_table.post_id
AND meta_key = "_wplp_post_front"
AND meta_value = 1
)

OK i think I found solution:
INSERT INTO wplw_postmeta (post_id, meta_key, meta_value)
SELECT post_id, '_woosea_brand', meta_value FROM wplw_postmeta WHERE meta_key='woocommerce_ceneo_Producent'

Related

How to update rows when WHERE value is a sub query with more than 1 row of results

I want to run this SQL:
UPDATE wp_postmeta
SET meta_value = 1.00
WHERE meta_key = '_test'
AND post_id = (SELECT post_id
FROM wp_postmeta
WHERE meta_value = 8032)
I was hoping it would set meta_value to 1.00 where the meta_key is _test and post_id is any of the returned post_id from the sub query: SELECT post_id FROM wp_postmeta WHERE meta_value = 8032
Upon simulating this query in phpMyAdmin it says that the sub query has more than 1 result, therefore how can I make this work?
Maybe it's not really something to do in SQL, I guess I could pull the post_id sub query results into PHP then loop through them and then do the original SQL..?
Replace = with IN:
UPDATE wp_postmeta
SET meta_value = 1.00
WHERE
meta_key = '_test'
AND post_id IN (SELECT post_id FROM wp_postmeta WHERE meta_value = 8032)
But as table is the same why not just:
UPDATE wp_postmeta
SET meta_value = 1.00
WHERE
meta_key = '_test'
AND meta_value = 8032
Error
Table 'wp_postmeta' is specified twice, both as a target for 'UPDATE' and as a separate source for data
can be fixed as
UPDATE wp_postmeta
SET meta_value = 1.00
WHERE
meta_key = '_test'
AND post_id IN (
SELECT * FROM (
SELECT post_id FROM wp_postmeta WHERE meta_value = 8032
) t
)

Need to update the meta_value for certain meta_key from wp_postmeta table where post_date (from wp_posts) < 'xxxx-xx-xx %'

I would like to update the meta_value for certain meta_key from wp_postmeta table where post_date (from wp_posts) < 'xxxx-xx-xx %'.
Such as I have some posts with meta_key(x_visibility) = meta_value(only_visible). Now I want to update all the "only_visible" to "hard_visible" for all the posts who has meta_key "only_visible" except the posts has posted last one month.
Thanks in Advance !
Finally I got the solution, hope will help others.
UPDATE wp_postmeta p, wp_posts a
SET p.meta_value = 'new meta_value'
WHERE
a.ID = p.post_id
AND a.post_date <= "2017-01-31"
AND p.meta_value = 'old meta_value';

wpdb - add up values for custom field within a specific category

I have a custom field called "points." This is a numerical value, so some posts have 2 points, some have 10, etc.
I want to count the total number of "points" for all of the posts within a given category who also have the custom field "cf_Category" with the value of "Chapter Building"
So, maybe query all the posts in Category X, whose value for meta key "cf_Category" is "Chapter Building" and then add up the values for the meta key "Points" and then echo that number.
Does that make sense? I'm able to add up all of the points for all posts, but am having trouble limiting it to one category.
Thanks in advance!
Sounds like you need a subquery. In SQL, you want something like this:
SELECT sum(meta_value) from wp_postmeta
WHERE meta_key = "points"
AND post_id in (SELECT object_id from wp_term_relationships
WHERE term_taxonomy_id = 6)
AND post_id in (SELECT post_id from wp_postmeta
WHERE meta_key = "cf_Category" and meta_value = "Chapter Building")
The inner query will return a list of post_ids that in the taxonomy with id 6. Those post ids are then used as a condition for the query that sums the points field.
For reference, the object_id field in the wp_term_relationships table is generally the same as a post's ID, and the term_taxonomy_id in that table is generally the same as the term_id. If you don't know the term_id of a category or tag, you can find it by looking at the URL of the page when you edit that category; it's in there as a parameter called "tag_ID".
If you're not familiar with writing custom SQL queries in Wordpress, the Codex has some details. You'll want to use $wpdb->prepare().
Final code per OP
<?php
$meta_key = 'cf_PointValue';
$point_total = $wpdb->get_var( $wpdb->prepare(
"
SELECT sum(meta_value) from wp_postmeta
WHERE meta_key = %s
AND post_id in (SELECT object_id from wp_term_relationships
WHERE term_taxonomy_id = 17)
AND post_id in (SELECT post_id from wp_postmeta
WHERE meta_key = 'cf_Category' and meta_value = 'Chapter Building')",
$meta_key
) );
echo $point_total;
?>
<?php
$meta_key = 'cf_PointValue';
$point_total = $wpdb->get_var( $wpdb->prepare(
"
SELECT sum(meta_value) from wp_postmeta
WHERE meta_key = %s
AND post_id in (SELECT object_id from wp_term_relationships
WHERE term_taxonomy_id = 17)
AND post_id in (SELECT post_id from wp_postmeta
WHERE meta_key = 'cf_Category' and meta_value = 'Chapter Building')",
$meta_key
) );
echo $point_total;
?>

SQL: Selecting value of cell based on the value of another cell

The WordPress database schema for Users is as follows:
TABLE wp_users
ID
user_login
user_pass
user_nicename
user_email
...
TABLE wp_usermeta
umeta_id
user_id
meta_key
meta_value
Now, it's the wp_usermeta table that I want to be able to write a SQL statement for.
In one row, meta_key is "first_name" ... how do I write a SQL query that will pull out the meta_value where meta_key is equal to "first_name"? This does not work:
SELECT meta_value FROM wp_usermeta WHERE meta_value = "first_name";
It should be
SELECT meta_value FROM wp_usermeta WHERE meta_key = "first_name";
Notice you had WHERE meta_value instead of WHERE meta_key

Sql query to show some results using PHP

at the end of this I want to display the TITLES of the results that have extra1 as meta_key and test as meta_value.
My goal is to:
From table wp_postmeta get the post_id of every row that has extra1 as meta_key and test as meta_value
Using the post_id, get the post_title for every row on step 1 respectively.
Print the post_title
Thank you very much.
this is the query assuming you are working on wordpress database schema:
SELECT post_title
FROM wp_posts
WHERE ID IN
(
SELECT DISTINCT post_id
FROM wp_postmeta
WHERE meta_key = 'extra1' AND meta_value = 'test'
)
second query after Mairy's comment:
SELECT post_title
FROM wp_posts
WHERE ID IN
(
SELECT DISTINCT post_id
FROM wp_postmeta
WHERE meta_key IN('extra1','extra2','extra3') AND meta_value IN('test','test1','test2','test3')
)
then you just need to loop over th result set with php and then print the post title as you prefer:
//connect to DB then ($query contains the query above)
$res = mysql_query($query, $db);
while($row=mysql_fetch_array($res))
{
echo($row['post_title']);
}

Categories