2017-06-13 6 views

答えて

0

フィルタータイプのウィジェットを作成する場合。ポストタイプごとにタグリストを取得する必要があります。

これについては、投稿タイプごとにタグやタクソノミーリストをリストすることで、一般的な関数を1つ作成しました。

関数get_terms_by_post_type($ taxo_term、$ post_types){

global $wpdb; 
$query = $wpdb->prepare(
          "SELECT term.*, COUNT(*) from $wpdb->terms AS term 
          INNER JOIN $wpdb->term_taxonomy AS texo ON term.term_id = texo.term_id 
          INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = texo.term_taxonomy_id 
          INNER JOIN $wpdb->posts AS post ON post.ID = tr.object_id 
          WHERE post.post_type IN('%s') AND texo.taxonomy IN('%s') 
          GROUP BY term.term_id", 
          join("', '", $post_types), 
          join("', '", $taxo_term) 
         ); 

$results = $wpdb->get_results($query); 
return $results; 

}

この関数を呼び出しと分類タームとポスト型を渡します。それはあなたにタグの配列を与えるでしょう。

3
<!-- begin custom related loop, isa --> 

<?php 

// get the custom post type's taxonomy terms 

$custom_taxterms = wp_get_object_terms($post->ID, 'your_taxonomy', array('fields' => 'ids')); 
// arguments 
$args = array(
'post_type' => 'your_custom_post_type', 
'post_status' => 'publish', 
'posts_per_page' => 3, // you may edit this number 
'orderby' => 'rand', 
'tax_query' => array(
    array(
     'taxonomy' => 'your_taxonomy', 
     'field' => 'id', 
     'terms' => $custom_taxterms 
    ) 
), 
'post__not_in' => array ($post->ID), 
); 
$related_items = new WP_Query($args); 
// loop over query 
if ($related_items->have_posts()) : 
echo '<ul>'; 
while ($related_items->have_posts()) : $related_items->the_post(); 
?> 
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php 
endwhile; 
echo '</ul>'; 
endif; 
// Reset Post Data 
wp_reset_postdata(); 
?> 
+0

私の質問は特定のポストタイプです。 1つの投稿ではありません。これは私の場合には役に立ちません –

+0

wp_get_object_terms($ post_ids、 'post_tag'、$ optional_args)にur post type値をpost _tag値として渡すと、あなたは結果を得る。親切にリンクを参照してくださいhttps://codex.wordpress.org/Function_Reference/wp_get_object_terms – priya

+0

@priyaあなたは適切にドキュメントを読む必要があります。あなたが与えたリンクは、 'post_tag'引数はタクソノミーでなければならないことを明確に述べています。そこに投稿タイプを渡すことはできません。 –

関連する問題