2009-07-20 14 views
4

私は 'technologies'という名前のカスタムタクソノミーを作成しましたが、カテゴリやタグでできるように複数の用語をクエリすることはできません。Wordpress 2.8の複数のカスタムタクソノミー用語をクエリしますか?

これらのquerysは、作業を実行します。ただし、正しく次の作業のいずれ

query_posts('tag=goldfish,airplanes'); 

query_posts('technologies=php'); 

を:

query_posts('technologies=php,sql'); 

query_posts('technologies=php&technologies=sql'); 

私の目標は: 'PHP' の技術を持つすべての投稿を表示し、 'SQLの技術を持つすべての記事

アイデア?これも可能ですか?ありがとう!

答えて

10

どうやらquery_postsは、この特定の状況で役立つことはできません。 (うまくいけば、それはWordpressの将来のバージョンで追加されます!)ソリューションは、次のようなカスタムSELECTクエリを使用することです:

SELECT * 
FROM $wpdb->posts 
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) 
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) 
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) 
WHERE $wpdb->posts.post_type = 'post' 
AND $wpdb->posts.post_status = 'publish' 
AND $wpdb->term_taxonomy.taxonomy = 'technologies' 
AND $wpdb->terms.slug = 'php' OR $wpdb->terms.slug = 'css' 
ORDER BY $wpdb->posts.post_date DESC 

詳しい情報は、Wordpressのコーデックスで見つけることができます: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

+0

上記のような手作業によるクエリは、まさに私が使い終わったものです。今は、複数のget_posts()クエリを1つずつ実行したくない場合(そしてそうでない場合)は、動作する唯一の方法です。 – bobsoap

+0

このクエリは、私がしようとしているものでうまくいくようですが、1つの投稿で複数の語句を検索したい場合はどうなりますか? ORをAND(クエリの9行目)に変更しようとしましたが、動作しないようです。 – jasonaburton

1

これは機能しますか? query_posts( 'タグ=パン+焼き+レシピ')

から:http://codex.wordpress.org/Template_Tags/query_posts

+0

残念ながら、query_posts( 'technologies = css + html');これらの用語の両方に関連する複数の投稿があるにもかかわらず、投稿は返されません。 –

0

にです何らかの形で、WPで独自の分類を実装した後に、自由に使用する組み込み関数はなく、ドキュメントは存在しないものに近いです。私は解決策を探していましたが、このクエリはそれを解決してくれました。ありがとう。

でも、悲しいことに私はあまりにも愚かであり(OOPブラインド)、関数にすることができないので、私はそれを繰り返しません。 私が手:**Fatal error**: Call to a member function get_results() on a non-object

私は関数内から$ wpdbを呼び出す方法がわからないと思います。

+0

「グローバル$ wpdb;」を追加する必要がありました。関数の中で。 愚かな私。上記のエントリを投稿した直後にそれを実現しました。 – pax

1

これは私の亀裂です。ちょっとハッキリですが、うまくいきます。大きな欠点は、複数の用語が呼び出されたときにfailがすべての照会変数を取り除くように、他の照会変数を再追加する必要があることです。

また、私は複数のタクソノミを照会することに対してこれをテストしませんでした。これは特定のタクソノミー内でのみ機能します。自己責任。

function multi_tax_terms($where) { 
    global $wp_query; 
    if (strpos($wp_query->query_vars['term'], ',') !== false && strpos($where, "AND 0") !== false) { 
     // it's failing because taxonomies can't handle multiple terms 
     //first, get the terms 
     $term_arr = explode(",", $wp_query->query_vars['term']); 
     foreach($term_arr as $term_item) { 
      $terms[] = get_terms($wp_query->query_vars['taxonomy'], array('slug' => $term_item)); 
     } 

     //next, get the id of posts with that term in that tax 
     foreach ($terms as $term) { 
      $term_ids[] = $term[0]->term_id; 
     } 

     $post_ids = get_objects_in_term($term_ids, $wp_query->query_vars['taxonomy']); 

     if (!is_wp_error($post_ids) && count($post_ids)) { 
      // build the new query 
      $new_where = " AND wp_posts.ID IN (" . implode(', ', $post_ids) . ") "; 
      // re-add any other query vars via concatenation on the $new_where string below here 

      // now, sub out the bad where with the good 
      $where = str_replace("AND 0", $new_where, $where); 
     } else { 
      // give up 
     } 
    } 
    return $where; 
} 

add_filter("posts_where", "multi_tax_terms"); 
0

それは次のようにする必要があります:

少なくとも、カスタムpost_typesのために働く
 global $wp_query; 
     query_posts(
       array_merge(
        array('taxonomy' => 'technologies', 'term' => array('sql', 'php')), 
        $wp_query->query 
       ) 
      ); 

http://scribu.net/wordpress/query-multiple-taxonomies/

4

あなたはこのプラグインを使用することができます。ここで作成wp_queryオブジェクトを介して

$loop = new WP_Query(array('technologies' => 'php','technologies' => 'sql')); 

その後のことができますループ:あなたは多くの複数の値を持っていない場合は、むしろ生のSQLクエリを記述するよりも、次のようにそれを行うことができます。 これは非常に古い投稿ですが、すでに問題を解決していることを確認しています。 :)

+0

+1:このプラグインはすぐに問題なく機能しました。 – thesunneversets

0

はねえ、私もかつて同じ問題に直面した:

6

これは少し遅れて返信されますが、Googleでは「WordPressの関連記事を複数の用語で表示する」のは初めてですので、私の所見を寄稿したいと思いました。

この質問が投稿されましたので、このタイプのクエリを許可するようWordpressが変更されました。これは、あなたのオブジェクトに割り当てられたカスタム分類用語のいずれかによって関連記事のリストが表示されます:

$post_cats = wp_get_object_terms(get_the_ID(), 'video_category', array('fields' => 'ids')); 

$args=array(
    "tax_query" => array(
     array(
      "taxonomy" => "video_category", 
      "field" => "id", 
      "terms" => $post_cats 
     ) 
    ), 
    'post__not_in' => array(get_the_ID()), 
    'post_type' => 'video', 
    'posts_per_page' => 8, 
    'caller_get_posts' => 1 
); 

$related_by_cats = new WP_Query($args); 

SO、私はそれが基準次第ですご希望にこれが私の最初の貢献です。

0
//equivalent to get_posts 
function brand_get_posts($args=array()){ 
global $wpdb; 
$sql = "SELECT p.* "; 
$sql.= " FROM $wpdb->posts p"; 
$sql.= " LEFT JOIN $wpdb->term_relationships term_r ON(p.ID = term_r.object_id)"; 
$sql.= " LEFT JOIN $wpdb->term_taxonomy term_t ON(term_r.term_taxonomy_id = term_t.term_taxonomy_id)"; 
$sql.= " LEFT JOIN $wpdb->terms terms ON(term_t.term_id = terms.term_id)"; 
$sql.= " WHERE 1=1 "; 
if(!empty($args['post_type'])){ 
    $sql.= " AND p.post_type = '".$args['post_type']."'"; 
} 
$sql.= " AND p.post_status = 'publish'"; 

if(!empty($args['taxonomy'])){ 
    $sql.= " AND term_t.taxonomy = '".$args['taxonomy']."'"; 
} 
if(!empty($args['terms'])&&is_array($args['terms'])){ 
    $sql.= " AND terms.slug IN ('".implode(",",$args['terms'])."')"; 
} 
$sql.= " ORDER BY p.post_date DESC"; 
if(!empty($args['posts_per_page'])){ 
    $sql.=" LIMIT ".$args['posts_per_page']; 
} 
if(!empty($args['offset'])){ 
    $sql.=" OFFSET ".$args['offset']; 
} 
//echo '<h1>'.$sql.'</h1>'; 
return $wpdb->get_results($sql); 
} 
関連する問題