当我在我的网站上搜索时,它目前不会在我的自定义元框中搜索内容。
搜索时如何包含此内容?
我正在注册我的元框,如下所示:
$meta_box[\'recipe\'] = array(
\'id\' => \'recipe-meta-details\',
\'title\' => \'Recipe Meta Details\',
\'context\' => \'normal\',
\'priority\' => \'high\',
\'fields\' => array(
array(
\'name\' => \'Country\',
\'desc\' => \'Country dish is from\',
\'id\' => \'recipe_country\',
\'type\' => \'text\',
\'default\' => \'\'
),
array(
\'name\' => \'Serves\',
\'desc\' => \'Number of people dish serves\',
\'id\' => \'recipe_serves\',
\'type\' => \'text\',
\'default\' => \'\'
),
array(
\'name\' => \'Ingredients\',
\'desc\' => \'\',
\'id\' => \'recipe_ingredients\',
\'type\' => \'wysiwyg\',
\'default\' => \'\'
),
array(
\'name\' => \'Method\',
\'desc\' => \'\',
\'id\' => \'recipe_method\',
\'type\' => \'wysiwyg\',
\'default\' => \'\'
)
)
);
并在我的自定义存档后循环中这样显示它们:
$recipe_country = get_post_meta($post->ID, \'recipe_country\', true);
$recipe_serves = get_post_meta($post->ID, \'recipe_serves\', true);
$recipe_ingredients = get_post_meta($post->ID, \'recipe_ingredients\', true);
$recipe_method = get_post_meta($post->ID, \'recipe_method\', true);
<div id="post-content-left">
<h3>Ingredients</h3>
<?php echo $recipe_ingredients; ?>
</div>
<div id="post-content-right">
<h3>Method</h3>
<?php echo $recipe_method; ?>
</div>
谢谢
SO网友:turbonerd
我最近有这个问题。我最终使用了WP_Query
修改我的网站的搜索结果。
我已经节略了一些代码,但本质上这就是我所做的。编辑主题的search.php
.
首先,我创建了一个名为“meta\\u query”的参数数组。如果您的自定义帖子类型附带了自定义分类法,那么您也可以对“tax\\u query”执行此操作。如果元框中有多个字段,则可能需要多个字段。
<?php
$aquarium_H = array(
\'key\' => \'aquarium_H\', //name of your meta field
\'value\' => $_GET["s"], // value from WordPress search bar. this is sanitized by WP
\'type\' => \'numeric\', // string/numeric/whatever
\'compare\' => \'<=\' // this can be "LIKE" or "NOT LIKE". most mySQL operators
);
$meta_query[] = $aquarium_H; // add to meta_query array
接下来,我检查这个数组是否存在,如果存在,我将它作为
meta_query
an的值
$args
大堆
if (isset($meta_query)) {
$args = array(
\'post_type\' => \'species\', // can be "any"
\'meta_query\' => $meta_query
);
}
?>
这意味着当我使用以下行时,搜索值(从
$_GET["s"]
, 它是WordPress搜索框的默认名称),在您指定的元框中查找。
<?php $query = new WP_Query ( $args ); ?>
然后需要根据主题对代码进行一些调整。在这一点上,我不认为“只是”
have_posts
将起作用-您需要指定$查询对象。
<?php if ($query->have_posts()) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<?php /* the_content could go here. I use different code */ ?>
</div>
<?php endwhile; ?>
<?php else : ?>
如果这有点让人困惑,我深表歉意!