0

カスタム投稿 (「ait-dir-items」と呼ばれます) と 2 つのカテゴリがあります。カテゴリ アーカイブ ページで、2 つのカテゴリに属する​​投稿を取得する必要があります。

2 つのカテゴリから投稿を取得する

コード。"ait-dir-item-category" の category1= "food" (食品 ID は 6) および "ait-dir-item-category" の category2= "cate" (食品 ID は 39) および post_type="ait-dir -アイテム」

私はこの問題を解決するために3つの方法を試してきました。それらのどれもうまく機能していません。どうすれば修正できるか教えてください。

第1の方法

query_posts("cat=6, 39&showposts=5&post_type=ait-dir-item");
//query_posts( array( post_type=>'ait-dir-item', 'category__and' => array(6,39),         'posts_per_page' => 12, 'orderby' => 'title', 'order' => 'DESC' ) );

while(have_posts()) : the_post();

echo $title = get_the_title();
echo $content = get_the_content();
endwhile;

「cat=6, 39」または「'category__and' => array(6,39)」を入力すると、結果が見つかりません。

2番目の方法

global $post;

$tmp_post = $post;

$args = array(
'posts_per_page' => 5,
'post_type' => 'ait-dir-item',
'category__and' => array(6, 39) // where 1, 2 is cat id
/*'tax_query' => array(
array(
'taxonomy' => 'ait-dir-item-special',
'field' => 'id',
'terms' => 39 // taxonomy id
)
)*/
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) :

$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail_size' );
$url = $thumb['0'];
?>

<li class="display_special_items"><img src="<? echo $url; ?>"/>"><?php the_title(); ?>
<?php endforeach; ?>
<?php $post = $tmp_post; ?>
<li class="clear">

第 3 の方法: 関係と AND


$custom_terms = get_terms('ait-dir-item-special');
$other_custom_terms = get_terms('ait-dir-item-category');

foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
wp_reset_query();
$args = array('post_type' => 'ait-dir-item',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'ait-dir-item-category',
'field' => 'id',
'terms' => 6
),
array(
'taxonomy' => 'ait-dir-item-special',
'field' => 'id',
'terms' => 39
),
),
);

$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h1 style="margin-top:10px;">'.$custom_term->name.'</h1>';

while($loop->have_posts()) : $loop->the_post();
echo '<h2>'.get_the_title().'</h2>';
endwhile;
}
}
}

` これらのコードにはほとんど問題がないと思います。表示されますが、重複してすべてのアイテムの投稿が表示されます。どのように修正すればよいですか?

ありがとう、

4

2 に答える 2

0

同様の状況があり、次の回答が見つかりませんでした。

  • custom_taxonomy1 に属し、かつ custom_taxonomy2 にも属する同じ custom_type のすべての投稿を取得します。

例: 特定の政党 (custom_taxonomy2) に属する SectorA (custom_taxonomy1) のすべての提案が必要でした。

私のケースでうまくいった次のクエリを使用することになりました。

SELECT 
wposts.ID, 
wposts.post_title, 
wposts.post_type, 
wposts.post_status,

termRels.object_id termObjectId, 
termRels.term_taxonomy_id termTaxId, 
terms.name partido

FROM $wpdb->posts wposts

INNER JOIN $wpdb->terms terms  
ON terms.term_id = $p 

INNER JOIN $wpdb->terms termsSector  
ON termsSector.term_id = $sector   

INNER JOIN $wpdb->term_taxonomy termTax 
ON termTax.term_id = terms.term_id 

INNER JOIN $wpdb->term_taxonomy termTax2  
ON termTax2.term_id = termsSector.term_id  

INNER JOIN $wpdb->term_relationships termRels  
ON termRels.term_taxonomy_id = termTax.term_taxonomy_id  

INNER JOIN $wpdb->term_relationships termRelsSector   
ON termRelsSector.term_taxonomy_id = termTax2.term_taxonomy_id  

WHERE wposts.post_type = 'likan_planes'

AND wposts.post_title != 'Auto Draft' 
AND wposts.post_status = 'publish' 

AND wposts.ID = termRels.object_id 
AND termRels.object_id = termRelsSector.object_id 

ORDER BY wposts.post_title ASC 
于 2013-10-25T15:30:36.953 に答える