Skip to content

Instantly share code, notes, and snippets.

@theJasonJones
Last active September 10, 2019 19:12
Show Gist options
  • Save theJasonJones/f0d56be0766a736a4ebe9da01a8a4c72 to your computer and use it in GitHub Desktop.
Save theJasonJones/f0d56be0766a736a4ebe9da01a8a4c72 to your computer and use it in GitHub Desktop.
WP Auto-submit select using wp_dropdown_categories. These usually go in the same file but i separated them for (my) clarity.
<?php
$category_id = htmlspecialchars($_GET['cat']);
if( !empty($category_id) ){
$args = array(
'posts_per_page' => -1,
'post_type' => 'testimonials',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'review-types',
'field' => 'id',
'terms' => $category_id,
),
),
);
}
else {
$args = array(
'posts_per_page' => -1,
'post_type' => 'testimonials',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_status' => 'publish'
);
}
$reviews = new WP_Query( $args );
if ( $reviews->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $reviews->have_posts() ) : $reviews->the_post(); ?>
<?php $image_check = pk_get_featured_image( $reviews->ID, 'circle' );
if( !empty( $image_check ) ):
?>
<div class="row">
<div class="col-xs-12 col-sm-3">
<img src="<?php echo $image_check ?>" alt="Reviews" class="img-responsive img-circle">
</div>
<?php endif; ?>
<?php $terms = get_the_terms($reviews->ID, 'review-types'); ?>
<?php if( !empty( $image_check ) ): ?>
<div class="col-xs-12 col-sm-9">
<?php else: ?>
<div class="review">
<?php endif; ?>
<div class="cats">
<?php
if( !empty( $terms ) ):
foreach( $terms as $check => $te ):
$size = sizeof($terms);
echo $te->name;
if( $check + 1 != $size ){ echo ", "; }
endforeach;
endif;
?>
</div>
<h3><?php the_title(); ?></h3>
<div class="excerpt">
<?php the_content(); ?>
</div>
</div>
<?php if( !empty( $image_check ) ): ?>
</div> <!-- /row -->
<div class="gray-border"></div>
<?php endif; ?>
<?php endwhile; ?>
<!-- end of the loop -->
<label>Filter By Category: </label>
<form id="category-select" class="category-select" action="" method="GET">
<?php
$cat = '';
if( isset($_GET['cat']) ){ $cat = htmlspecialchars( $_GET['cat'] ); }
$tag ='';
$args = array(
'taxonomy' => 'review-types',
'show_option_all' => __( 'View All' ),
'show_count' => 0,
'selected' => $cat,
'orderby' => 'name',
'echo' => 0,
);
?>
<?php $select = wp_dropdown_categories( $args ); ?>
<?php $replace = "<select$1 onchange='return this.form.submit()'>"; ?>
<?php $select = preg_replace( '#<select([^>]*)>#', $replace, $select ); ?>
<div class=" custom-select">
<?php echo $select; ?>
</div>
<noscript>
<input type="submit" value="View" />
</noscript>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment