2008-08-26 6 views
6

이것은 와 같은 매우 구체적인 질문입니다.WordPress으로 구현되었습니다. 나는 (여러번) '를 태그의 특정을 가지고'카테고리을 '특정에 속하는 게시물 (선택)이 표시됩니다 플러그인을 개발하기 위해 노력하고있어WordPress의 특정 태그/카테고리가있는 게시물을 선택하는 방법

나는 때문에 불가능 들었다 카테고리와 태그가 저장되는 방식 :

  1. wp_posts 게시물의 목록을 포함, 각 게시물은 "ID"
  2. wp_terms이 용어 목록 (모두 categorie이 포함되어 있습니다 s 및 태그). EAC 용어는
  3. wp_term_taxonomy가 TERM_IDs와 용어의 목록을 가지고 있으며 (카테고리 또는 태그 중 하나)
  4. wp_term_relationships는 협회의 betweens 규정 및 게시물
있다을 그 각각의 분류 정의가있는 TERM_ID있다

표를 결합하여 "Nuclear"태그가있는 모든 게시물을 얻으려면 어떻게해야합니까? 또한 "카테고리 1"범주에 속하는 "거래"?

답변

3

오해했습니다. 핵이나 거래를 원한다고 생각했는데. 아래는 당신에게 핵과 거래만을 줄 것입니다.

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') 
2

어떤 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" 
              ) 
          ) 
      ) 
1

이 시도 ... 당신은 몇 가지 추가 저글링 하나의 방법을 수행 할 수 있습니다 또는 다른 그것이 작동하도록 :

기본적으로 나는 적절한 자식 테이블 2 부 사용하고있어
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')) 

- 용어를 , term_taxonomy 및 term_relationship. 한 부는 'Category1'제한을 적용하고, 다른 하나는 'Nuclear'또는 'Deals'제한을 적용합니다.

그런데, 핵 거래에 관한 모든 게시물은 어떤 프로젝트입니까? 정부 목록에 우리를 데려다 려구요? ;)

1

그래서 내 WordPress 데이터베이스에서 두 옵션을 시도했다. 나는 "Perl"태그와 "Programming"태그를 사용하여 "Tech"카테고리를 내 게시물에서 찾았습니다.

Eric's 한 번 작동했습니다. 초기 선택문에 누락 된 쉼표를 추가했습니다. 3 개의 레코드가 반환되었습니다. 문제는 "post_tag"를 찾고있는 섹션이 실제로 OR 옵션으로 작동한다는 것입니다. 내 게시물 중 하나에 둘 다 태그가 하나만있는 것은 아닙니다. 또한 SELECT DISTINCT를 만드는 것이 좋습니다.

Matt's 버전을 시도했지만 빈 세트가 계속 반환되었습니다. 나는 그것으로 "저글링"하려고 할 것입니다.

0

감사합니다. @Eric 작동합니다!나중에 참조 할 수 있도록 몇 가지 코드 수정 :

wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship 

tr2

것은해야한다 :

  • 첫 번째 선택 문

    같은 선택 statemt에서
  • 가 변화해야 다음 wp_term_relationship의 TR2 후 혼수 상태를 벗어났습니다

    wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship 
    
    tr3
0

정말 대단한 대답 .. 많이 도와주었습니다.

위대한 bcoz., 내게 복잡한 쿼리를 작성하는 기본 접근 방식을 준! 나 같은 준비가 사용자

하나의 작은 보정 :

"wp_term_relationship".. 을 '오류가 존재하지 않는'올바른 테이블 이름으로 wp_term_relationships를 사용하여 줄 것이다.

감사합니다. Eric

관련 문제