If you want to customize your search page and allow the search of custom fields, you can easily achieve this. In the sample below we are using 2 custom fields. The sample above is for a bar directory. The bars have different specials everyday and are located in different cities. The default wordpress search does not come with additional form fields, but you can follow this tutorial to create a custom search for your WordPress website that uses Custom Fields.

The first thing you need to do is add the additional fields to the form.

What we want to do is add two form fields for each of the values. In my case I am tracking day, special and city.

So you can visit this link on creating form fields.

Once you create your form fields and name them, go to your search.php file and capture the new values to a variable, $_GET[‘your field name’];
[php]
$dayofweek = $_GET[‘dayofweek’];
$specialoffer = $_GET[‘specialoffer’];
$post_city_id = $_GET[‘city’];

[/php]

You can then pass those values to your query –
[php]
$args = array(
‘meta_query’ => array(
‘relation’ => ‘AND’,
array(
‘key’ => $dayofweek,
‘value’ => $specialoffer,

),
array(
‘key’ => post_city_id,
‘value’ => $post_city_id,

)
)
);
[/php]

Full code below.

[php]
<?php
$dayofweek = $_GET[‘dayofweek’];
$specialoffer = $_GET[‘specialoffer’];
$post_city_id = $_GET[‘city’];

if ($dayofweek && $specialoffer) {
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$args = array(
‘meta_query’ => array(
‘relation’ => ‘AND’,
array(
‘key’ => $dayofweek,
‘value’ => $specialoffer,

),
array(
‘key’ => post_city_id,
‘value’ => $post_city_id,

)
)
);
query_posts($args);
} else {
query_posts(‘posts_per_page=4’);
}

if ($dayofweek && $specialoffer) { ?>
<h3>Your Search For <?php echo $dayofweek; ?> and <?php echo $post_city_id; ?></h3>
<?php } else { ?>
<h3>Title</h3>
<?php } if(have_posts()) : ?>
<?php while(have_posts()) : the_post()
?>

<p class="no_percentage"><?php echo excerpt(15); ?> </p>

<span class="readmore" ><a href="<?php the_permalink(); ?>" > <?php _e(‘read more’);?> </a> </span>

<?php endwhile; ?>

<?php else: ?>
<p class="notice_msg"><?php _e( ‘Sorry, but nothing matched your search criteria.’); ?></p>
<?php endif; ?>

[/php]

This is Part 1 of Custom Field search customization with WordPress. What happens when there are more fields to search ? This search is still not perfect and with the current query you have limitations. We will go over next time on Custom Field Search Part 2 and provide you a more advance way of doing the samething with additional options.