我已经为我的主题创建了一个自定义搜索页面,该页面必须在我的CPT非层次分类法中进行搜索,如果通过自定义字段的选项表单插入了某些首选项,则最终会过滤结果。
我有和高级searchform。php这只是一个表单
<input type="hidden" name="search" value="advanced">
<input type="text" value="" placeholder="<?php _e( \'Cosa c\\\'è in frigo?\', \'whattodo\' ); ?>" name="ing" id="ing" />
并为此类post自定义字段选择三个。
<select name="tempo" id="tempo">
<select name="tempo" id="portata">
<select name="tempo" id="difficolta">
所有这些都会进入高级搜索结果。php代码,加上页面底部的查询循环,我现在没有将其包括在内。
<?php
// Get data from URL into variables
$_ing = $_GET[\'ing\'] != \'\' ? $pieces = explode(" ", $_GET[\'ing\']) : \'\';
$_tempo = $_GET[\'tempo\'] != \'\' ? $_GET[\'tempo\'] : \'\';
$_portata = $_GET[\'portata\'] != \'\' ? $_GET[\'portata\'] : \'\';
$_difficolta = $_GET[\'difficolta\'] != \'\' ? $_GET[\'difficolta\'] : \'\';
// VERIFY IF SOMETHING WAS TYPED IN SEARCH
if( ! empty( $pieces ) ){
//BUILDING ARRAY FOR TAX_QUERY RELATIONS
foreach( $pieces as $pezzo ){
$tag[] = array(
\'taxonomy\' => \'ingredienti\',
\'terms\' => "\'".$pezzo."\'",
\'field\' => \'slug\');
};
}
// Start the Query
$v_args = array(
\'post_type\' => \'ricetta\', // your CPT
\'tax_query\' => array(
\'relation\' => \'AND\',
$tag,
),
\'meta_query\' => array(
array(
\'key\' => \'tempo_di_realizzazione\', \'value\' => $_tempo,
\'compare\' => \'LIKE\',
),
array(
\'key\' => \'portata\',
\'value\' => $_portata,
\'compare\' => \'LIKE\', ),array(
\'key\' => \'difficolta\', \'value\' => $_difficolta,
\'compare\' => \'LIKE\', )
)
);
$ricettaSearchQuery = new WP_Query( $v_args );
// Open this line to Debug what\'s query WP has just run
var_dump($ricettaSearchQuery->request);
这就是问题所在。如果我在索引中运行这种tax\\u查询。php页面没有自定义的元字段,一切都很顺利,我得到了我期望的帖子。但是如果我在这个表单中运行这个,我什么也找不到,就像wordpress无法读取这个页面上的帖子分类法一样。
我附加查询的var\\u dump()。
string(813) "SELECT SQL_CALC_FOUND_ROWS wtdx_posts.ID FROM wtdx_posts INNER JOIN wtdx_postmeta ON ( wtdx_posts.ID = wtdx_postmeta.post_id ) INNER JOIN wtdx_postmeta AS mt1 ON ( wtdx_posts.ID = mt1.post_id ) INNER JOIN wtdx_postmeta AS mt2 ON ( wtdx_posts.ID = mt2.post_id ) WHERE 1=1 AND ( ( 0 = 1 ) ) AND ( ( wtdx_postmeta.meta_key = \'tempo_di_realizzazione\' AND CAST(wtdx_postmeta.meta_value AS CHAR) LIKE \'%%\' ) AND ( mt1.meta_key = \'portata\' AND CAST(mt1.meta_value AS CHAR) LIKE \'%contorno%\' ) AND ( mt2.meta_key = \'difficolta\' AND CAST(mt2.meta_value AS CHAR) LIKE \'%%\' ) ) AND wtdx_posts.post_type = \'ricetta\' AND (wtdx_posts.post_status = \'publish\' OR wtdx_posts.post_author = 1 AND wtdx_posts.post_status = \'private\') GROUP BY wtdx_posts.ID ORDER BY wtdx_posts.post_date DESC LIMIT 0, 10" Nessuna Ricetta Trovata