5

'technologies'という名前のカスタム分類法を作成しましたが、カテゴリやタグのように複数の用語をクエリすることはできません。

これらのクエリは機能します:

query_posts('tag=goldfish,airplanes');

query_posts('technologies=php');

ただし、次のいずれも正しく機能しません。

query_posts('technologies=php,sql');

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

私の目的:「php」のテクノロジーを使用したすべての投稿と「sql」のテクノロジーを使用したすべての投稿を表示する

何か案は?これも可能ですか?ありがとう!

4

9 に答える 9

10

どうやらquery_postsはこの特定の状況では役に立ちません。(Wordpressの将来のバージョンで追加されることを願っています!)解決策は、次のようなカスタム選択クエリを使用することです。

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 Codexをご覧ください:http: //codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

于 2009-07-21T21:40:16.493 に答える
6

これは少し遅れた返信ですが、「複数の用語によるワードプレス関連の投稿」については、現時点でGoogleで初めてなので、調査結果に貢献したいと思いました。

この質問が投稿されてから、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への私の最初の貢献です、私はそれが標準に達していることを願っています。

于 2011-12-17T19:05:54.280 に答える
4

このプラグインを使用できます:

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

于 2010-07-09T12:35:26.537 に答える
1

これは機能しますか?query_posts('tag = bread + bakeing + recipe')

差出人:http: //codex.wordpress.org/Template_Tags/query_posts

于 2009-07-20T23:03:21.340 に答える
1

さて、これが私の亀裂です。少しハッキーですが、機能します。大きな欠点は、他のクエリ変数を再度追加する必要があることです。複数の用語が呼び出されると、失敗するとすべてのクエリ変数が削除されます。

また、複数の分類法にわたるクエリに対してこれをテストしませんでした。これは、特定の分類法内でのみ機能します。自己責任。

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");
于 2010-01-13T22:28:20.023 に答える
0

次のようになります。

        global $wp_query;
        query_posts(
                array_merge(
                    array('taxonomy' => 'technologies', 'term' => array('sql', 'php')),
                    $wp_query->query
                )
            );

少なくとも、カスタムpost_typesで機能します。

于 2010-06-22T18:29:53.903 に答える
0

WPにカスタム分類法を実装した後、それらを自由に使用するための組み込み関数がなく、ドキュメントがほとんど存在しないのは、どういうわけかばかげています。私は解決策を探していました、このクエリはそれを解決します(そして私の一日を作りました)。ありがとう。

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

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

于 2009-10-11T23:27:50.997 に答える
0

ねえ、私も一度同じ問題に直面しました。複数の値が多くない場合は、生のSQLクエリを作成するのではなく、次の方法で実行できます。

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

次に、ここで作成したwp_queryオブジェクトをループできます。これは非常に古い投稿であり、すでに問題を解決していると確信していますが。:)

于 2011-06-03T19:33:32.923 に答える
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);
 }
于 2013-10-22T08:44:27.070 に答える