関連記事を出してみる。
つくった。
カスタム投稿用
タクソノミーを使った記事の出力。
自身が持つタクソノミーと同一のIDどれか1つでも持っている記事を自身を除く$post_count件をランダムで出力する。
//関連記事の取得 カスタム投稿用
function get_related_custom_posts($post_id,$post_type,$post_count){
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
foreach($taxonomies as $taxonomy){
$terms[$taxonomy->name] = get_the_terms($post_id,$taxonomy->name);
}
$tax_query['relation'] = 'OR';
foreach($terms as $term_name => $term_array){
//$tax_query[$term_name] = $term_array;
foreach($term_array as $term){
$tax_query[][] = array(
'taxonomy' => $term_name,
'field' => 'term_id',
'terms' => $term->term_id,
);
}
}
$args['posts_per_page'] = $post_count;
$args['post_type'] = $post_type;
$args['tax_query'] = $tax_query;
$args['post_status'] = 'publish';
$args['post__not_in'] = array($post_id);
$args['orderby'] = 'rand';
$related_post = new WP_Query( $args );
return $related_post->posts;
}