• RSS

[WP]カスタム投稿タイプで特定のタームスの記事を取得して表示する方法

  • このエントリーをはてなブックマークに追加
  • follow us in feedly

WordPress のtax_query() を使って、カスタム投稿タイプの特定のターム(カテゴリ)の記事を絞り込んで表示させる方法です。

目次

カスタム投稿タイプ、カスタムタクソノミーが1つずつの場合

<?php
$arg = array (
	'post_type' => 'カスタム投稿名',
	'post_status' => 'publish',
	'posts_per_page'=> -1,
	'tax_query' => array(
		array(
			'taxonomy' => 'カスタムタクソノミー名',
			'field' => 'slug',
			'terms' => array('タームス1','タームス2','タームス3')
		)
	)
);?>
<?php $the_query = new WP_Query( $arg );?>


カスタム投稿タイプ、カスタムタクソノミーが各々2つの場合

<?php
$arg = array (
	'post_type' => array('カスタム投稿名1','カスタム投稿名2'),
	'post_status' => 'publish',
	'posts_per_page'=> -1,
	'tax_query' => array(
		array(
			'taxonomy' => 'カスタムタクソノミー名1',
			'field' => 'slug',
			'terms' => array('タームス1','タームス2','タームス3')
		),
		array(
			'taxonomy' => 'カスタムタクソノミー名2',
			'field' => 'slug',
			'terms' => array('タームス4','タームス5','タームス6')
		),
		'relation' => 'AND'
	)
);?>
<?php $the_query = new WP_Query( $arg );?>



  • このエントリーをはてなブックマークに追加
  • follow us in feedly

コメント

コメントを残す