Change meta_value relating to specific meta_key - mysql

I have a wordpress directory theme in which a bunch of listings are about to expire. The were all created on different days. I want to batch update the whole database so that all my listings are reset for another year as of today.
They related to the table wp_postmeta and the meta_key is "alive_days" and I need to update the pertaining meta_value.
If anyone can suggest an SQL query to fix this, I will be saved a from pulling out my hair.
Thanks in advance.
LLG
See http://snag.gy/KfJfB.jpg for screengrab of the database.

Based on your response to my question, this should do the trick (take a backup of your database first, just in case):
update wp_postmeta
set meta_value = '365' -- I assume you meant that, not 356
where meta_key = 'alive_days'
The existing blanks will be updated, but since they relate to posts that no longer exist, that shouldn't matter. If you don't want to update them, you could add an extra condition (I assume they contain the empty string, not null, but the ifnull should handle that):
update wp_postmeta
set meta_value = '365'
where meta_key = 'alive_days'
and ifnull(meta_value, '') != ''

Related

Check 2 tables, if match then update third table

I have 2 tables that have data in them, due to the size of the third table I don't want to run queries against it so I have created a secondary table with only product SKU and IDs.
Here is what I am trying to do but I am quite confused on where to start.
Optimistically there is a way to do this with a MySQL query and loop through all of the records.
if wp_stc_outofstock `product_number` == wp_stc_current_products `sku`
UPDATE wp_postmeta SET meta_value = 'outofstock' WHERE post_id = 'wp_stc_outofstock.product_number' AND meta_key = '_stock_status'
Any help is appreciated, I am sure I am not doing a great job explaining it.
My best attempt given your limited explanation -
UPDATE `wp_stc_outofstock` `oos`
JOIN `wp_stc_current_products` `cp`
ON `oos`.`product_number` = `cp`.`sku`
JOIN `wp_postmeta` `pm`
ON `oos`.`product_number` = `pm`.`post_id`
AND `pm`.`meta_key` = '_stock_status'
SET `pm`.`meta_value` = 'outofstock';
As #danblack suggested, perhaps you should be dealing with your performance concerns, particularly by looking at your indexing. As long as the tables are indexed appropriately, millions of rows is not an issue.

Delete wp users based on role and registration date

I have about 180000 users in my wp_users database. Most of them are old and dormant. I fear this is slowing down my site (is this likely?). I want to safely delete old accounts along with all their associated data.
Using bulk delete plugins does not work as they timeout with so many users. I want to do it with mysql instead. By searching around I have found the following code:
DELETE
wp_users,
wp_usermeta
FROM
wp_users
INNER JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_id
WHERE
meta_key = 'wp_capabilities' AND
meta_value LIKE '%subscriber%' AND
user_registered < NOW() - INTERVAL 360 DAY
The code works but is it safe and is it the best way? Will it delete their associated meta data etc.
Please first take a backup of your database before running this query
Yes You can use this query to delete the user & user_meta data but if your theme/plugin saving users data in some others table(if you are using any plugin that store user data in other table) than you have to look again in database and modify your query according to this..

Update Table from Another Table where column IN multiple values

My first attempt at this failed and I had to do a pesky restore on a really large table.
I've tried multiple queries, I think I am getting close, but everything I find provides a syntax error (I'm assuming because of the IN clause)
The tables are identical, but wp_postmeta_price only contains the value I want to restore _regular_price
Here's what the wp_postmeta table looks like
Lots of different columns here of various sorts.
The values in this table I'm trying restore I created another table wp_postmeta_prices with ONLY the meta_value of _regular_price I'm wanting to restore based from the meta_key and meta_id
Here's what that looks like
This may not be the easiest way, but what I did was export this table to an excel spreadsheet and used a tool to create the post_id column to a csv format so that I could use the IN clause for multiple unique instances
Here's maybe the closest I've gotten
UPDATE wp_postmeta
INNER JOIN wp_postmeta_price ON wp_postmeta.post_id = wp_postmeta_price.post_id
AND wp_postmeta.meta_id = wp_postmeta_price.meta_id
SET wp_postmeta.meta_value = wp_postmeta_price.meta_value WHERE '_regular_price' IN (2498,2443,2424,2518)
but this is not correct.
Can anyone please help me with the proper syntax, or enlighten me on a better possible solution for this?

Wordpress SQL data query layout

I am using SQL Executioner in Wordpress Admin and trying to get a list of data from a table (wp_postmeta) using the following:
SELECT post_id, meta_value from wp_postmeta
WHERE meta_key = "_wprm_reservation_name"
UNION
(SELECT post_id, meta_value from wp_postmeta
WHERE meta_key = "_wprm_reservation_phone_number");
This returns the information that I require but the layout is wrong. I am getting two columns: one with the post_id number and meta_value (persons name) listed then after all those I am getting the post_id number repeated and the telephone number.
Ideally I am trying to get the phone number to be placed next to the persons name. The post_id is the key as the number is used for both the name and phone number.
I have tried number variations trying to understand the UNION statement but cannot get this to work. Can anyone advise please where I am going wrong here?
Union working like this, see the documentation. What you need is a subquery, or a join. This is with subquery:
SELECT wppm.post_id, wppm.meta_value,
(SELECT wppmphone.meta_value FROM wp_postmeta wppmphone
WHERE wppmphone.meta_key = "_wprm_reservation_phone_number"
AND wppmphone.post_id = wppm.post_id) AS phoneNum
FROM wp_postmeta wppm WHERE meta_key = "_wprm_reservation_name";
This is one query, I just separated the subquery by new lines to be readable. This is not tested, but it should like something like this.
Note, I am using aliases for the table names.

Wordpress SQL delete posts and their related meta

I have the current SQL query which will delete all posts from a custom post type clothing which are older than 2 days
delete
p,pm
from wp_posts p
join wp_postmeta pm on pm.post_id = p.id
where p.post_type = 'clothing'
and DATEDIFF(NOW(), p.post_date) > 2
The problem is that this query doesn't seem to delete the related metas such as related custom fields of the deleted posts.
My question is, how can I modify this code to also delete the relate metas from those posts?
Thanks
You should process in two steps :
1- with you query, build an array of posts to delete
2- loop this array with foreach and wp_delete_post()
(http://codex.wordpress.org/Function_Reference/wp_delete_post)
The core function wp_delete_post will take care of all related data, like metas, but also counts of posts in terms, wich are stored in the database and modified on insert or deletion of posts