php 【WordPress的】复数のタクソノミを条件に使った复合検索をする

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 【WordPress的】复数のタクソノミを条件に使った复合検索をする相关的知识,希望对你有一定的参考价值。

<div class="container">
	<div id="main-grid2" class="row">
		<div id="primary" class="content-area col-md-12">
			<main id="main" class="site-main" role="main">
				<div class="menu_title2">
					<div class="menu_title_en2">
						<span class="glyphicon glyphicon-list-alt glypad"></span>検索結果一覧<span class="menu_smalltitle">Results</span>
					</div>
				</div>
				<div class="container">
<?php
	$paged = (int) get_query_var('paged');
	$args = array(
					$query_string,
					'posts_per_page' => 12,
					'post_type'      => array( 'ex,', 'inex' ),
					'orderby'        => 'post_date',
					'order'          => 'DESC',
					'post_status'    => 'publish',
					'paged'          => $paged
				);
	if ( $_GET['recruit-tags'] !== '' ) {
		$recruit_tags = array(
							'taxonomy' => 'recruit-tags',
							'field' => 'slug',
							'terms' => $_GET['recruit-tags'],
						);
	} else {
		$recruit_tags = '';
	}

	if ( $_GET['industry'] !== '' ) {
		$industry = array(
						'taxonomy' => 'industry',
						'field' => 'slug',
						'terms' => $_GET['industry'],
					);
	} else {
		$industry = '';
	}

	if ( $_GET['occupation'] !== '' ) {
		$occupation = array(
						'taxonomy' => 'occupation',
						'field' => 'slug',
						'terms' => $_GET['occupation'],
					);
	} else {
		$occupation = '';
	}

	if ( $_GET['area'] !== '' ) {
		$area = array(
					'taxonomy' => 'area',
					'field' => 'slug',
					'terms' => $_GET['area'],
				);
	} else {
		$area = '';
	}

	if ( !empty( $recruit_tags ) || !empty( $industry ) || !empty( $occupation ) || !empty( $area ) ) {
		$args['tax_query'] = array(
								'relation' => 'OR', // いわゆる「絞り込み」なら 'AND'、「複合検索」は 'OR'
								$recruit_tags,
								$industry,
								$occupation,
								$area
							);
	}

	$the_query = new WP_Query( $args );

	if ( $the_query -> have_posts() ) :
		if ( !empty( $recruit_tags ) ) {
			$term_name[] = get_term_by( 'slug', $recruit_tags['terms'], 'recruit-tags' ) -> name;
		}

		if ( !empty( $industry ) ) {
			$term_name[] = get_term_by( 'slug', $industry['terms'], 'industry' ) -> name;
		}

		if ( !empty( $occupation ) ) {
			$term_name[] = get_term_by( 'slug', $occupation['terms'], 'occupation' ) -> name;
		}

		if ( !empty( $area ) ) {
			$term_name[] = get_term_by( 'slug', $area['terms'], 'area' ) -> name;
		}

	if ( !empty( $recruit_tags ) || !empty( $industry ) || !empty( $occupation ) || !empty( $area ) ) {
		$term_names = implode( "、", $term_name );
	}

		if ( $term_names ) :
?>
					<p>「<?php echo esc_attr( $term_names ); ?>」の検索結果</p>
	<?php else : ?>
					<p>すべての検索結果</p>
	<?php endif; ?>
					<ul class="row">
	<?php
		while ( $the_query -> have_posts() ) :
			$the_query -> the_post();
	?>
<?php get_template_part( 'inc/loop-li' ); // 出力したいコンテンツ部分 ?>
	<?php endwhile; ?>
					</ul>
	<?php if ( function_exists( 'wp_pagenavi' ) ) : ?>
					<div id="wp-pagenavi">
						<?php wp_pagenavi( array( 'query' => $the_query ) ); ?>
					</div>
<?php
		endif;

		wp_reset_postdata();
	else :
?>
					<p>申し訳ございませんが、検索条件に該当する案件はございません。</p>
<?php endif; ?>
				</div>
			</main>
		</div>
	</div>
</div>
<div class="row">
	<form method="get" action="<?php echo home_url( 'search-results/' ); ?>" target="_blank" class="search-form">
		<div class="col-xs-12 col-md-11 row">
			<div class="pulldown col-xs-12 col-md-3">
				<p>求人タグから探す</p>
<?php
	$terms = get_terms( 'recruit-tags' );
	if ( $terms ) :
?>
					<select name="recruit-tags" class="tagssearch">
						<option value="">---</option>
	<?php
		foreach ( $terms as $term ) :
			$term_slug = $term -> slug;
			$term_title = $term -> name;
	?>
						<option value="<?php echo esc_attr( $term_slug ); ?>"><?php echo esc_attr( $term_title ); ?></option>
	<?php endforeach; ?>
					</select>
<?php endif; ?>
			</div>

			<div class="pulldown col-xs-12 col-md-3 ml_50">
				<p>職種から探す</p>
<?php
	$terms = get_terms( 'occupation' );
	if ( $terms ) :
?>
					<select name="occupation" class="occupationsearch">
						<option value="">---</option>
	<?php
		foreach ( $terms as $term ) :
			$term_slug = $term -> slug;
			$term_title = $term -> name;
	?>
						<option value="<?php echo esc_attr( $term_slug ); ?>"><?php echo esc_attr( $term_title ); ?></option>
	<?php endforeach; ?>
					</select>
<?php endif; ?>
			</div>

			<div class="pulldown col-xs-12 col-md-3 ml_50">
				<p>業種から探す</p>
<?php
	$terms = get_terms( 'industry' );
	if ( $terms ) :
?>
					<select name="industry" class="industrysearch">
						<option value="">---</option>
	<?php
		foreach ( $terms as $term ) :
			$term_slug = $term -> slug;
			$term_title = $term -> name;
	?>
						<option value="<?php echo esc_attr( $term_slug ); ?>"><?php echo esc_attr( $term_title ); ?></option>
	<?php endforeach; ?>
					</select>
<?php endif; ?>
			</div>

			<div class="pulldown col-xs-12 col-md-3 ml_50">
				<p>エリアから探す</p>
<?php
	$terms = get_terms( 'area' );
	if ( $terms ) :
?>
					<select name="area" class="areasearch">
						<option value="">---</option>
	<?php
		foreach ( $terms as $term ) :
			$term_slug = $term -> slug;
			$term_title = $term -> name;
	?>
						<option value="<?php echo esc_attr( $term_slug ); ?>"><?php echo esc_attr( $term_title ); ?></option>
	<?php endforeach; ?>
					</select>
<?php endif; ?>
			</div>
		</div>
		<div class="col-xs-11 col-md-1 search-btn">
			<div class="search-btn-flow">
				<button type="submit" class="btn">検索</button>
			</div>
		</div>
	</form>
</div>

以上是关于php 【WordPress的】复数のタクソノミを条件に使った复合検索をする的主要内容,如果未能解决你的问题,请参考以下文章

php タクソノミーの选択をラジオボタンにする

sh 直近のタグからHEADまでのマージコミットの体(PRのタイトル)の一覧を表示するコマンド

java [Java] FilenameFilterを使ったフィルタクラス。

python アルゴリズムクイックリファレンス:クイックソート

text 20180320属性のターゲティング

javascript メニューのタブ切り替え