我会保持这个超级简洁的正常搜索工作得很好,但当我为其他功能添加了一些复选框等时,tax\\u查询不起作用,但我知道$\\u GET正在工作,因为我已将结果输出到页面左上角进行测试。
实时站点:http://costaricalifestyleproperties.com.s221952.gridserver.com/
if(!empty($_GET[\'ct_lifestyle\'])) {
//if(is_array($_GET[\'feature\'])) {
$ct_lifestyle = $_GET[\'ct_lifestyle\'];
foreach ($ct_lifestyle as $lifestyle):
$search_values[\'tax_query\'] = array (
\'taxonomy\' => \'lifestyle\',
\'field\' => \'slug\',
\'terms\' => $lifestyle,
\'operator\' => \'IN\'
);
echo $lifestyle;
endforeach;
//}
}
我为测试注释掉了is\\u array(),可以选择一个复选框而不是多个复选框。自定义分类法也可以使用,我对名称进行了双重和三重检查。下面是寄存器函数。
非常感谢您的帮助,我已经绞尽脑汁了好几个小时,我知道这一定是我错过的简单的东西。
如果(!function\\u exists(\'ct\\u lifestyle\\u taxonomies\')){
add_action( \'init\', \'ct_lifestyle_taxonomies\', 0 );
function ct_lifestyle_taxonomies() {
// Lifestyle
$lifestylelabels = array(
\'name\' => __( \'Lifestyle\', \'contempo\' ),
\'singular_name\' => __( \'Lifestyle\', \'contempo\' ),
\'search_items\' => __( \'Search Lifestyles\', \'contempo\' ),
\'popular_items\' => __( \'Popular Lifestyles\', \'contempo\' ),
\'all_items\' => __( \'All Lifestyles\', \'contempo\' ),
\'parent_item\' => null,
\'parent_item_colon\' => null,
\'edit_item\' => __( \'Edit Lifestyle\', \'contempo\' ),
\'update_item\' => __( \'Update Lifestyle\', \'contempo\' ),
\'add_new_item\' => __( \'Add New Lifestyle\', \'contempo\' ),
\'new_item_name\' => __( \'New Lifestyle Name\', \'contempo\' ),
\'separate_items_with_commas\' => __( \'Separate Lifestyles with commas\', \'contempo\' ),
\'add_or_remove_items\' => __( \'Add or remove Lifestyles\', \'contempo\' ),
\'choose_from_most_used\' => __( \'Choose from the most used Lifestyles\', \'contempo\' )
);
register_taxonomy( \'lifestyle\', \'listings\', array(
\'hierarchical\' => false,
\'labels\' => $lifestylelabels,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'lifestyle\' ),
));
if(!function_exists(\'lifestyle\')) {
function lifestyle() {
global $post;
global $wp_query;
$terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, \'lifestyle\', \'\', \', \', \'\' ) );
echo esc_html($terms_as_text);
}
}
}
}
SO网友:Jacob Peattie
此处:
$search_values[\'tax_query\'] = array (
您正在重置
tax_query
对于
foreach
环
它也被设置为invalid value:
Important Note: tax\\u查询采用array 共个税务查询参数arrays (它需要一组数组)。此构造允许您使用relation
第一个(外部)数组中的参数,用于描述分类法数组之间的布尔关系。
所以要建立一个tax_query
对于多个阵列,您需要设置$search_values[\'tax_query\']
到空数组,然后附加每个数组:
if( ! empty( $_GET[\'ct_lifestyle\'] ) ) {
$ct_lifestyle = (array) $_GET[\'ct_lifestyle\'];
$search_values[\'tax_query\'] = array();
foreach ($ct_lifestyle as $lifestyle):
$search_values[\'tax_query\'][] = array (
\'taxonomy\' => \'lifestyle\',
\'field\' => \'slug\',
\'terms\' => $lifestyle,
);
endforeach;
}
还要注意这一点:
$ct_lifestyle = (array) $_GET[\'ct_lifestyle\'];
将其转换为数组意味着如果只有一个
ct_lifestyle
参数存在,则它将与单个项一起放入一个数组中。这样你的
foreach
在您不必以不同方式处理单个项目的情况下仍能正常工作。
该代码假设您希望查询匹配具有给定“寿命类型”的所有帖子。这就是我删除IN
operator
从您的代码。如果您想匹配具有给定生活方式的帖子,可以跳过foreach
将数组传递到tax_query
:
if( ! empty( $_GET[\'ct_lifestyle\'] ) ) {
$ct_lifestyle = (array) $_GET[\'ct_lifestyle\'];
$search_values[\'tax_query\'] = array(
array(
\'taxonomy\' => \'lifestyle\',
\'field\' => \'slug\',
\'terms\' => $ct_lifestyle,
\'operator\' => \'IN\',
)
);
}