Добавление пользовательских полей ввода в поиск WordPress

Моя цель состоит в том, чтобы добавить 2 поля ввода в форму поиска WordPress, Имя и Фамилия. Каждый из них будет показывать результаты публикации для своих уважаемых пользовательских метаполей из типа «услуги». Но по какой-то причине, когда я тестировал его, он не показывает ни одну из публикаций с этим пользовательским метаполем.

Вот код, который у меня есть в моем файле searchform.php.

<?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 &hellip;" 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>

Вот весь код в моем файле search.php:

<?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();

Буду признателен за любую помощь.

0

Решение

пытаться:

 $meta_query = array();
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'] );
}

$search = new WP_Query( array(
'post_type' => 'service',
'meta_query' => $meta_query
) );

Кроме того, поскольку вы используете wp_query, ваш цикл должен использовать переменную, в которой вы сохранили свой запрос ….

if (!$search->have_posts()) :
echo '<div class="error_block">' .
'<h2>' . __('Nothing found. Try another search?', 'cmsmasters') . '</h2>';
get_search_form();
echo '</div>';
else :
while ($search->have_posts()) : $search->the_post();
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector