WordPress で特定のタグ/カテゴリを持つ投稿を選択する方法
質問
これは非常に具体的な質問です MySQL で実装されているように ワードプレス.
特定の ' を持つ投稿を表示 (選択) するプラグインを開発しようとしています。タグ' および特定の ' に属しますカテゴリ' (両方とも複数)
カテゴリとタグの保存方法が次のとおりであるため、それは不可能だと言われました。
wp_posts
投稿のリストが含まれており、各投稿には「ID」が付いています。wp_terms
用語のリスト (カテゴリとタグの両方) が含まれています。各用語には TERM_ID がありますwp_term_taxonomy
TERM_ID を含む用語のリストがあり、それぞれの分類定義 (カテゴリまたはタグ) があります。wp_term_relationships
用語と投稿の間に関連性があります
「核」タグが付いたすべての投稿を取得するには、テーブルに参加するにはどうすればよいですか? そして カテゴリ「Category1」にも属する「取引」ですか?
解決
私はあなたのことを誤解していました。あなたは核か取引を望んでいるのだと思いました。以下では、Nuclear と Deals のみが表示されます。
select p.*
from wp_posts p, wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr,
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
where p.id = tr.object_id and t.term_id = tt.term_id and tr.term_taxonomy_id = tt.term_taxonomy_id
and p.id = tr2.object_id and t2.term_id = tt2.term_id and tr2.term_taxonomy_id = tt2.term_taxonomy_id
and p.id = tr3.object_id and t3.term_id = tt3.term_id and tr3.term_taxonomy_id = tt3.term_taxonomy_id
and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name = 'Nuclear')
and (tt3.taxonomy = 'post_tag' and tt3.term_id = t3.term_id and t3.name = 'Deals')
他のヒント
なんとも壮大な DB 構造です。
とにかく、私は次のようなことをします(私は結合よりもEXISTSを好むことに注意してください。しかし、必要に応じて結合として書き直すことができます。ほとんどのクエリ アナライザーは、とにかくそれらを同じクエリ プランに折りたたみます)。それを機能させるには、何らかの方法で追加のジャグリングを行う必要があるかもしれません...
SELECT *
FROM wp_posts p
WHERE EXISTS( SELECT *
FROM wp_term_relationship tr
WHERE tr.object_id = p.id
AND EXISTS( SELECT *
FROM wp_term_taxonomy tt
WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
AND tt.taxonomy = 'category'
AND EXISTS( SELECT *
FROM wp_terms t
WHERE t.term_id = tt.term_id
AND t.name = "Category1"
)
)
AND EXISTS( SELECT *
FROM wp_term_taxonomy tt
WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
AND tt.taxonomy = 'post_tag'
AND EXISTS( SELECT *
FROM wp_terms t
WHERE t.term_id = tt.term_id
AND t.name = "Nuclear"
)
AND EXISTS( SELECT *
FROM wp_terms t
WHERE t.term_id = tt.term_id
AND t.name = "Deals"
)
)
)
そこで、WordPress データベースで両方のオプションを試しました。「Perl」と「プログラミング」のタグが付いた投稿で、カテゴリ「Tech」を探しました。
エリックさんの 最初の選択ステートメントに欠落しているカンマを追加すると機能しました。3 つのレコードが返されました。問題は、「post_tag」を探しているセクションが実際には OR オプションとして機能していることです。私の投稿の 1 つにタグが 1 つだけあり、両方にはありませんでした。また、SELECT DISTINCT を作成するとよいでしょう。
私は試した マットさんの バージョンを変更しましたが、空のセットが返され続けました。私はそれを「やりくり」しようとするかもしれません。
これを試して:
select p.*
from wp_posts p,
wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
where p.id = tr.object_id
and t.term_id = tt.term_id
and tr.term_taxonomy_id = tt.term_taxonomy_id
and p.id = tr2.object_id
and t2.term_id = tt2.term_id
and tr2.term_taxonomy_id = tt2.term_taxonomy_id
and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name in ('Nuclear', 'Deals'))
基本的に、関連する子テーブルの 2 つのコピー (terms、term_taxonomy、term_relationship) を使用しています。1 つのコピーには「Category1」制限が適用され、もう 1 つのコピーには「Nuclear」または「Deals」制限が適用されます。
ところで、核合意に関する投稿ばかりを集めたこのプロジェクトは一体何なのでしょうか?私たちを政府のリストに載せようとしているのですか?;)
ありがとう@Eric それはうまくいきます!今後の参考のためにコードをいくつか修正します。
- 最初の select ステートメントには、wp_term_relationship tr2 の後のカンマがありません。
- 同じ選択ステートメント内で、以下を変更する必要があります。
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship
tr2
あるべきです
wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship
tr3
本当に素晴らしい答えです..とても助けられました。
素晴らしいですね。複雑なクエリを構築するための基本的なアプローチが得られました。
私のような準備ができているユーザーのために、小さな修正が 1 つあります:)
「wp_term_relationship」は「存在しないエラー」を与えます。使用 wp_term_relationships それは正しいテーブル名であるためです。
ありがとうエリック