Skip to content

Instantly share code, notes, and snippets.

@tarikcayir
Created August 2, 2015 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarikcayir/31fafe66ec1e613f679e to your computer and use it in GitHub Desktop.
Save tarikcayir/31fafe66ec1e613f679e to your computer and use it in GitHub Desktop.
WordPress taxonomy query
<?php
/**
* The template used for displaying page content
*
* @package WordPress
* @subpackage Twenty_Fifteen
* @since Twenty Fifteen 1.0
*/
$postCity = $_POST['city'];
$postJob = $_POST['job'];
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
// Post thumbnail.
twentyfifteen_post_thumbnail();
?>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<form action="http://localhost/ustapro.com/listeleme-sayfasi/" method="POST">
<select name="city" id="city">
<option value=""><?php _e('--- Sec ---','twentyfifteen'); ?></option>
<?php
//list terms in a given taxonomy
$taxonomy = 'category';
$tax_terms = get_terms($taxonomy);
?>
<?php foreach ($tax_terms as $tax_term): ?>
<?php
if( $tax_term->slug == $postCity){
$selected = 'selected';
}else{
$selected = '';
}
?>
<option value="<?=$tax_term->slug ?>" <?=$selected?>><?=$tax_term->name ?></option>
<?php endforeach; ?>
</select>
<br/>
<select name="job" id="job">
<option value=""><?php _e('--- Sec ---','twentyfifteen'); ?></option>
<?php
//list terms in a given taxonomy
$taxonomy = 'post_job';
$tax_terms = get_terms($taxonomy);
?>
<?php foreach ($tax_terms as $tax_term): ?>
<?php
if( $tax_term->slug == $postJob){
$selected = 'selected';
}else{
$selected = '';
}
?>
<option value="<?=$tax_term->slug ?>" <?=$selected?>><?=$tax_term->name ?></option>
<?php endforeach; ?>
</select>
<br/>
<input type="submit" class="submit" value="Gönder">
</form>
<?php
if ( $postCity ){
$taxQueryCity = array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $postCity,
);
}else{
$taxQueryCity = array();
}
if ( $postJob ){
$taxQueryJob = array(
'taxonomy' => 'post_job',
'field' => 'slug',
'terms' => $postJob,
);
}else{
$taxQueryJob = array();
}
?>
<?php $the_query = new WP_Query( array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 10,
'tax_query' => array(
$taxQueryCity,
$taxQueryJob)
) ); ?>
<?php if ( $the_query->have_posts($the_query) ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h4><?php echo get_the_title(); ?></h4>
<?php endwhile; endif; wp_reset_postdata(); ?>
</div><!-- .entry-content -->
<?php edit_post_link( __( 'Edit', 'twentyfifteen' ), '<footer class="entry-footer"><span class="edit-link">', '</span></footer><!-- .entry-footer -->' ); ?>
</article><!-- #post-## -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment