Skip to content

Instantly share code, notes, and snippets.

@nguyenvanduocit
Created July 25, 2018 22:31
Show Gist options
  • Save nguyenvanduocit/143ed81b73ccd4b80b52f73b0878350f to your computer and use it in GitHub Desktop.
Save nguyenvanduocit/143ed81b73ccd4b80b52f73b0878350f to your computer and use it in GitHub Desktop.
<?php
/**
* Template Name: Advanced Search
*/
get_header(); ?>
<?php
$min_price = $_GET['min_price'] ?? '';
$max_price = $_GET['max_price'] ?? '';
?>
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<form class="advanced-search-form">
<div class="form-group">
<label for="">Min Price</label>
<input type="number" name="min_price" value="<?php echo esc_attr($min_price); ?>">
</div>
<div class="form-group">
<label for="">Max Price</label>
<input type="number" name="max_price" value="<?php echo esc_attr($max_price); ?>">
</div>
<div class="form-group">
<input type="submit" value="Search">
</div>
</form>
<?php if ( $min_price || $max_price ): ?>
<div class="search-result">
<?php
$args = [
'posts_per_page' => - 1,
'meta_query' => []
];
if ( $min_price ) {
$args['meta_query'][] = [
'key' => 'hcf_price',
'value' => $min_price,
'compare' => '>=',
'type' => 'NUMERIC'
];
}
if ( $max_price ) {
$args['meta_query'][] = [
'key' => 'hcf_price',
'value' => $max_price,
'compare' => '<=',
'type' => 'NUMERIC'
];
}
$search_query = new WP_Query( $args );
if ( $search_query->have_posts() ):
while ( $search_query->have_posts() ) {
$search_query->the_post();
get_template_part( 'template-parts/post/content', 'excerpt' );
}
wp_reset_postdata();
?>
<?php else: ?>
<p>No result found.</p>
<?php endif; ?>
</div>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
<?php get_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment