Skip to content

Instantly share code, notes, and snippets.

@wokamoto

wokamoto/gist:7765880

Last active Dec 30, 2015
Embed
What would you like to do?
[WordPress] 親カテゴリー「おやおや」とその子カテゴリー「こどもも」があるとします。WordPressでは子カテゴリー「こどもも」にチェックを入れた場合、親カテゴリー「おやおや」にはチェックが入りません。これを親カテゴリーにも自動的にチェックを入れるプラグイン(Parent Category Toggler)はあるのですが、これを過去分にさかのぼってチェックを入れるには?
INSERT INTO wp_term_relationships
SELECT object_id, term_taxonomy_id, 0 AS term_order
FROM (
SELECT DISTINCT r.object_id, p.term_taxonomy_id
FROM wp_term_relationships AS r
INNER JOIN wp_term_taxonomy AS t ON r.term_taxonomy_id = t.term_taxonomy_id
INNER JOIN wp_term_taxonomy AS p ON t.parent = p.term_id
WHERE t.taxonomy = 'category'
AND t.parent > 0 ) AS a
WHERE NOT EXISTS
(SELECT 'x'
FROM wp_term_relationships AS b
WHERE a.object_id = b.object_id
AND a.term_taxonomy_id = b.term_taxonomy_id)
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.