我的目标是在Wordpress搜索表单中添加2个输入字段,名字和姓氏。每个人都会在帖子类型“服务”中显示他们尊重的自定义元字段的发布结果。但出于某种原因,当我测试它时,它没有显示任何具有该自定义元字段的帖子。
这是我在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 …" 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 :(得分: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();