WP_Queryのカスタマイズ事例(1)Destinationパターン

今回は、投稿データに関連付けられたタクソノミーや別の投稿を表示する方法について紹介します。

WP_Queryは、あるデータの集合(これをエンティティと呼びます)の外部キーから別のエンティティの主キーを検索しデータ群(オブジェクト)取得する仕組み(クラス)です。
 WordPressの場合のエンティティは、「投稿タイプ」「カスタム投稿」「カテゴリー」「ターム」「カスタムタクソノミー」などのデータの集合です。これらのエンティティ同士は、主キーと外部キーで関連付けられます。

下図は、ブログ投稿の外部キーにカスタムタクソノミー「ジャンル」が関連づけられている場合を図示したものです。

Destinationパターン

リスト37209は、投稿のカスタムフィールドに関連付けられた投稿の表示の事例です。
ブログ記事のような投稿データに、別の投稿データを関連づけて表示したい場合があります。

リスト37211は、投稿に関連付けられたタクソノミーの表示の事例です。
ブログ記事のような投稿データには、タクソノミー(カテゴリー、タグ)を関連づけて記事の分類分けをしますが、このとき、ブログ記事のタイトルの上部や本文の末尾などにタクソノミーを表示したい場合があります。

ブログ投稿(Post)とジャンル(Term)のER図

参考文献

参考記事

※1WP_Queryのカスタマイズ事例(2)Sourceパターン( meta_queryとtax_query)

この記事を参照している記事

WP_Queryのカスタマイズ事例(4)万能型サブクエリー

コード例

事例1:Destinationパターン(Post→Post)

class code37209
{
	public function __construct()
	{	
		$user = new Model37209() ;
	}//endfunction
}//endclass

class Model37209 extends WP_Query
{
	public function __construct()
	{
		$args = array(		
			'post_type' => 'post',
			'post__in' => array('18348'),//IDを配列で指定。
		);
		$the_query = new WP_Query($args) ;		

//ループ~表示処理(Viewクラス)の呼び出し。
		if($the_query->have_posts()){
			While($the_query->have_posts()){
				$the_query->the_post();
					$view_obj = new View37209($par) ;
			}
		}//endif
	}//endfunction

}//endclass

class View37209
{
	public function __construct($par)//$parはいらない。
	{
		the_title() ;
		echo "<br>\n" ;
		
//次の処理の準備
//投稿に関連付けられているカスタムフィールドのIDを取得する。
	$myposts = get_post_meta(get_the_ID(), 'ref', false) ;//'ref'を指定。
	if (!empty($myposts)){
		$par->foreign_key = $myposts ;
		$user = new Model37209a($par) ;
	}else{
		return ;
	}
		
	}//endfunction
}//endclass

class Model37209a
{
	public function __construct($par)
	{	
		$args1 = array(
			'post__in' => $par->foreign_key,//配列をそのまま記述
			'post_type' => 'reference',//DPP
		);
		$the_query = new WP_Query($args1) ;//WP_Query	

//ループ~表示処理(Viewクラス)の呼び出し。
		if($the_query->have_posts()){
			While($the_query->have_posts()){
				$the_query->the_post();
					$view_obj = new View37209a($par) ;
			}
		}//endif
	}//endfunction

}//endclass

class View37209a
{
	public function __construct($par)
	{

		the_title() ;
		echo "<br>\n" ;
		
	}//endfunction
}//endclass
築地(西仲通り商店街)明治時代の埋め立て工事により出来上がった町。

Warning: Creating default object from empty value in /home/xs663544/kokontouzai.jp/public_html/wp-content/plugins/wp-OOP/wp-OOP-CodeSample.php on line 781
第八章 月島の商業
月島再発見学

事例2:Destinationパターン(Post→Term)

class code37211
{
	public function __construct()
	{	
		$user = new Model37211() ;
	}//endfunction
}//endclass

class Model37211
{
	public function __construct()
	{
		$args = array(		
			'post_type' => 'post',
			'post__in' => array('18348'),//IDを配列で指定。
		);
		$the_query = new WP_Query($args) ;		

//ループ~表示処理(Viewクラス)の呼び出し。
		if($the_query->have_posts()){
			While($the_query->have_posts()){
				$the_query->the_post();
					$view_obj = new View37211($par) ;
			}
		}//endif
	}//endfunction

}//endclass

class View37211
{
	public function __construct($par)
	{
		the_title() ;
		echo "<br>\n" ;

//次の処理の準備
//投稿に関連付けられているタクソノミーのIDを取得する。
			$myterms = wp_get_object_terms(get_the_ID(), 'region');
			if (!empty($myterms)){
				$foreign_key = array() ;
				foreach ($myterms as $post){
					array_push( $foreign_key , $post->term_id);
				}
			}else{
				return ;
			}//endif		
		
		$par->foreign_key = $foreign_key ;
		$user = new Model37211a($par) ;
		
	}//endfunction
}//endclass

class Model37211a
{
	public function __construct($par)
	{

		$args = array(		
			'include' => $par->foreign_key,
		);
		$the_query = get_terms('region', $args) ;		

//ループ~表示処理(Viewクラス)の呼び出し。
		foreach ($the_query as $term){
			$par->term = $term ;
				$view_obj = new View37211a($par) ;			
		}
	}//endfunction

}//endclass

class View37211a
{
	public function __construct($par)
	{
		echo $par->term->name."<br>\n" ;
		
	}//endfunction
}//endclass
築地(西仲通り商店街)明治時代の埋め立て工事により出来上がった町。

Warning: Creating default object from empty value in /home/xs663544/kokontouzai.jp/public_html/wp-content/plugins/wp-OOP/wp-OOP-CodeSample.php on line 703
月島3丁目

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です