开发者

Update one Wordpress SQL Table Based on Information from Another

I am trying to run an SQL command on a Wordpress database. I want to update certain entries in wp_6_posts table's post-status column with the value "trash". The entries I want to update are the ones in table wp_6_term_relationships where the term_taxonomy_id = 13.

Then, for all those matches, I want to take the object_id from wp_6_term relationships and match it to the ID column in wp_6_posts.

Then I want to update only the wp_6_posts rows where the ID value matches the object_id value from the wp_6_terms_relationships table.

This is the closest I've come, but it just sets the post_status value of ALL posts in wp_6_posts t开发者_如何学Goo "trash". I know I just have the syntax or a bit of logic off.

UPDATE wp_6_posts   
SET post_status = "trash"
WHERE EXISTS
  ( SELECT object_id
    FROM wp_6_term_relationships
    WHERE term_taxonomy_id = "13"
    AND object_id = ID
)


UPDATE wp_6_posts   
SET post_status = "trash"
WHERE id IN
  ( SELECT object_id
    FROM wp_6_term_relationships
    WHERE term_taxonomy_id = "13"
  )


This is the answer that works for me, given to me by someone better at SQL. This query is using the category slug instead of the category ID #. I don't understand it fully, but it works--it gives all posts in category "sold" the post_status of "trash".

update wp_posts set post_status = 'trash'
where `id` in
(SELECT object_id
FROM wp_term_relationships wtr
JOIN wp_term_taxonomy wtt ON ( wtr.term_taxonomy_id = wtt.term_taxonomy_id ) 
JOIN wp_terms wt ON ( wtt.term_id = wt.term_id ) 
WHERE wtt.taxonomy = 'category'
AND wt.slug = 'sold')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜