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.

This post was written by

Frederick Pohl – who has written posts on Hire WordPress Developer | WordPress Theme Developer | WordPress Experts Developers - Wp1stop.
Frederick Pohl is a WordPress Developer and 2001 University of Florida Graduate in Electronic Intermedia. He has been specializing in Web Development and Graphic Design since 1997. He has spent the last 7 years focusing in providing WordPress custom services to a Worldwide clientele based out of Gainesville, FL. We provide complex customizations and theme design. I work 1on1 with all my clients and offer a top quality service. Request a Free quote and Hire us for your WordPress project.

Email  • Google +

Leave a Reply

You must be logged in to post a comment.