Archived topic
modify query by form input
2 replies · Started by Aaron on March 5, 2022
I need help connecting some puzzle peaces and push my boundaries on WordPress.
I know how to build a template file powered by a custom query.
Since we have a project with GP Pro, we use a Block - Content Template for Archive Pages, and I prefer to keep it that way than coding an archive template from scratch.
I would like to modify the query on archive pages based on a form to filter query results, and found examples on how to do that.
Is there a way to hook into GP Pro (or WordPress) to display a form and modify the query?
Hi there,
if you can implement your query form to modify the results returned by the themes archive template, then you cracked it :)
The Block Element - Content Template simply replaces the content template part inside the loop. So it doesn't really care what the loop query is.
if you can implement your query form to modify the results returned by the themes archive template, then you cracked it 🙂
That's exactly the point where I am stuck. After you reply, I took a look inside gp's archive.php and think of something like the code below 🤔 but I thought there must be some kind of hook for the query form code first. I'll give it a try.
<?php
/**
* The template for displaying Archive pages.
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
get_header(); ?>
<div <?php generate_do_attr('content'); ?>>
<main <?php generate_do_attr('main'); ?>>
<?php
/**
* generate_before_main_content hook.
*
* @since 0.1
*/
do_action('generate_before_main_content');
if (generate_has_default_loop()) {
//custom code starts here
if ($_GET['minprice'] && !empty($_GET['minprice'])) {
$minprice = $_GET['minprice'];
} else {
$minprice = 0;
}
if ($_GET['maxprice'] && !empty($_GET['maxprice'])) {
$maxprice = $_GET['maxprice'];
} else {
$maxprice = 999999;
}
if ($_GET['size'] && !empty($_GET['size'])) {
$size = $_GET['size'];
}
if ($_GET['color'] && !empty($_GET['color'])) {
$color = $_GET['color'];
} ?>
<div class="container">
<form action="/" method="get">
<label>min:</label>
<input type="number" name="minprice" value="<?php echo $minprice; ?>">
<label>max:</label>
<input type="number" name="maxprice" value="<?php echo $maxprice; ?>">
<label>Size:</label>
<select name="size">
<option value="">Any</option>
<option value="s">S</option>
<option value="m">M</option>
<option value="l">L</option>
<option value="xl">XL</option>
</select>
<label>Color:</label>
<select name="color">
<option value="">Any</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="yellow">Yellow</option>
<option value="purple">Purple</option>
<option value="black">Black</option>
</select>
<button type="submit" name="">Filter</button>
</form>
<?php
$args = [
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => [
[
'key' => 'price',
'type' => 'NUMERIC',
'value' => [$minprice, $maxprice],
'compare' => 'BETWEEN',
],
[
'key' => 'size',
'value' => $size,
'compare' => 'LIKE',
],
[
'key' => 'color',
'value' => $color,
'compare' => 'LIKE',
],
],
];
$query = new WP_Query($args);
if ($query->have_posts()) {
/*
* generate_archive_title hook.
*
* @since 0.1
*
* @hooked generate_archive_title - 10
*/
do_action('generate_archive_title');
/*
* generate_before_loop hook.
*
* @since 3.1.0
*/
do_action('generate_before_loop', 'archive');
while ($query->have_posts()) {
the_post();
generate_do_template_part('archive');
}
wp_reset_query();
/*
* generate_after_loop hook.
*
* @since 2.3
*/
do_action('generate_after_loop', 'archive');
} else {
generate_do_template_part('none');
}
}
/*
* generate_after_main_content hook.
*
* @since 0.1
*/
do_action('generate_after_main_content');
?>
</main>
</div>
<?php
/**
* generate_after_primary_content_area hook.
*
* @since 2.0
*/
do_action('generate_after_primary_content_area');
generate_construct_sidebars();
get_footer();