Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.