Yesterday I asked this question but I didn’t get answer. So I think people don’t understand my question. So Here I edit and explain it in details:
My WordPress have a post type Products
I Products have 2 custom field name Color & Location The Products also have enabled Category & Tags Field.
Now my search from look like this:
<form action="/" method="get">
<label>Keyword: </label> <input type="text" name="s" placeholder="Keyword"> <br />
<label>Color: </label> <input type="text" name="color" placeholder="Color"> <br />
<label>Location: </label> <input type="text" name="location" placeholder="Location"> <br />
<label>Category: </label> <select name="category"><option value="1">Shirt</option><option value="1">Pant</option><option value="1">Pajama</option></select> <br />
<label>Tags: </label> <input type="text" name="location" placeholder="Tags"> <br />
<input type="Submit" value="Search">
</form>
So If someone type shirt in keyword field and write Red in color field, then Search Result will show posts by shirt in title & red from meta_key from any category..
If people leave blank keyword field and input Red & Bangladesh in Color & Location field then only posts will come that contain Red & Bangladesh Meta value from any category.
So People can filter search by color, location, category & Tags.
I tried using this kind of search query in search.php but not work.
<?php
$color = $_GET['color'];
$location = $_GET['location'];
$args = array(
'post_type' => 'any',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => color,
'value' => $color,
),
array(
'key' => location,
'value' => $location,
)
)
);
?>
<h3>Your Search For <?php echo $color; ?> <?php if(!empty($location) OR isset($location) OR $location !== NULL ){echo '';} else { echo "and".$location; } ?></h3>
<?php $query = new WP_Query( $args ); if(have_posts()) : ?>
<?php while(have_posts()) : the_post() ?>
<h3><?php the_title(); ?></h3>
<?php if(get_field('color')): ?>
<b>Color:</b> <?php the_field('color'); ?>
<?php endif; ?>
<?php if(get_field('location')): ?>
<b>Location:</b> <?php the_field('location'); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else: ?>
<p class="notice_msg"><?php _e( 'Sorry, but nothing matched your search criteria.'); ?></p>
<?php endif; ?>
Please help me someone. I really need this Help.
Thank you
You need to correct is color in
'key' => color,
and location in'key' => location
, they should be strings like ‘color’ and ‘location’ otherwise they will be treated as constants, also add compare parameter in each criteria.e.g.