My goal is to add 2 input fields to the Wordpress search form, First Name and Last Name. Each one will show the post results for their respectful custom meta fields from the post type "services". But for some reason, when I tested it out, it's not showing any of the posts that have that custom meta field.
Here's the code that I have so far in my searchform.php file.
<?php
$meta_query = array();
if( !empty( $_GET['first_name'] ) ) {
$meta_query[] = array( 'key' => 'first_name', 'value' => $_GET['first_name'] );
}
if( !empty( $_GET['last_name'] ) ) {
$meta_query[] = array( 'key' => 'last_name', 'value' => $_GET['last_name'] );
}
$search = new WP_Query( array(
'post_type' => 'service',
'meta_query' => $meta_query
) );
?>
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" <?php if(is_search()) { ?>value="<?php the_search_query(); ?>" <?php } else { ?>value="Enter keywords …" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"<?php } ?> /><br />
<label>First Name</label><input type="text" name="first_name" />
<br />
<label>Last Name</label><input type="text" name="last_name"/>
<input type="submit" id="searchsubmit" value="Search" />
</form>
Here's all the code in my search.php file:
<?php
/**
* @package WordPress
* @subpackage Law Business
* @since Law Business 1.0
*
* Search Page Template
* Created by CMSMasters
*
*/
if( $_GET['first_name'] ) {
$meta_query[] = array( 'key' => 'first_name', 'value' => $_GET['first_name'] );
}
if( $_GET['last_name'] ) {
$meta_query['relation'] = 'OR';
$meta_query[] = array( 'key' => 'last_name', 'value' => $_GET['last_name'] );
}
get_header();
$cmsms_option = cmsms_get_global_options();
$cmsms_layout = $cmsms_option[CMSMS_SHORTNAME . '_search_layout'];
if (!$cmsms_layout) {
$cmsms_layout = 'r_sidebar';
}
echo '<!--_________________________ Start Content _________________________ -->' . "\n";
if ($cmsms_layout == 'r_sidebar') {
echo '<section id="content" role="main">' . "\n\t";
} elseif ($cmsms_layout == 'l_sidebar') {
echo '<section id="content" class="fr" role="main">' . "\n\t";
} else {
echo '<section id="middle_content" role="main">' . "\n\t";
}
?>
<div class="entry-summary">
<section class="blog">
<?php
if (!have_posts()) :
echo '<div class="error_block">' .
'<h2>' . __('Nothing found. Try another search?', 'cmsmasters') . '</h2>';
get_search_form();
echo '</div>';
else :
while (have_posts()) : the_post();
if (get_post_type() == 'post') {
if (get_post_format() != '') {
$cmsms_service_format = get_post_meta(get_the_ID(), 'cmsms_service_format', true);
}
} elseif (get_post_type() == 'service') {
$cmsms_service_format = get_post_meta(get_the_ID(), 'cmsms_service_format', true);
if (!$cmsms_service_format) {
$cmsms_service_format = 'slider';
}
?>
<article id="post-<?php the_ID(); ?>" <?php post_class('format-' . $cmsms_service_format); ?>>
<?php
cmsms_heading(get_the_ID(), 'service');
if (has_post_thumbnail()) {
cmsms_thumb(get_the_ID(), 'post-thumbnail', true, false, true, false, true, true, false);
}
the_excerpt();
cmsms_more(get_the_ID(), 'service');
?>
</article>
<div class="divider"></div>
<?php
} elseif (get_post_type() == 'page') {
?>
<article id="post-<?php the_ID(); ?>" <?php post_class('format-page'); ?>>
<?php
cmsms_heading(get_the_ID());
if (has_post_thumbnail()) {
cmsms_thumb(get_the_ID(), 'post-thumbnail', true, false, true, false, true, true, false);
}
the_excerpt();
?>
<div class="entry-content">
<h6><?php _e('This page contains your query', 'cmsmasters'); ?></h6>
</div>
<footer class="entry-meta">
<?php cmsms_more(get_the_ID()); ?>
</footer>
</article>
<div class="divider"></div>
<?php
}
endwhile;
pagination();
endif;
?>
</section>
</div>
</section>
<!-- _________________________ Finish Content _________________________ -->
<?php
if ($cmsms_layout == 'r_sidebar') {
echo "\n" . '<!-- _________________________ Start Sidebar _________________________ -->' . "\n" .
'<section id="sidebar" role="complementary">' . "\n";
get_sidebar();
echo "\n" . '</section>' . "\n" .
'<!-- _________________________ Finish Sidebar _________________________ -->' . "\n";
} elseif ($cmsms_layout == 'l_sidebar') {
echo "\n" . '<!-- _________________________ Start Sidebar _________________________ -->' . "\n" .
'<section id="sidebar" class="fl" role="complementary">' . "\n";
get_sidebar();
echo "\n" . '</section>' . "\n" .
'<!-- _________________________ Finish Sidebar _________________________ -->' . "\n";
}
get_footer();
I would appreciate any help.