【发布时间】:2019-10-30 01:33:35
【问题描述】:
我想用元字段创建过滤器我用 ACF 插件创建元字段,我想为某些元字段使用或关联,但它们不起作用,
例如,在过滤器上,我为元密钥培训师选择“男性”和“女性”,这显示没有帖子,但有一篇帖子的元值为男性,但我的查询不显示它们,但如果我只选择“男性”,则此查询是显示帖子
帖子中的元值是array('Male')
这是用于wordpress上的ajax过滤器
$args = [
'posts_per_page' => - 1,
];
if ( $minlength ) {
$min_lenght_query[] = [
'key' => 'duration',
'value' => $minlength,
'compare' => '>=',
'type' => 'NUMERIC'
];
}
if ( $maxlength ) {
$max_lenght_query[] = [
'key' => 'duration',
'value' => $maxlength,
'compare' => '<=',
'type' => 'NUMERIC'
];
}
if ( $minburn ) {
$minburn_query[] = [
'key' => 'calorie_burn_minimum',
'value' => $minburn,
'compare' => '>=',
'type' => 'NUMERIC'
];
}
if ( $maxburn ) {
$maxburn_query[] = [
'key' => 'calorie_burn_maximum',
'value' => $maxburn,
'compare' => '<=',
'type' => 'NUMERIC'
];
}
if ( $difficulty ) {
$difficulty_query[] = [
'key' => 'difficulty',
'value' => $difficulty,
'compare' => 'IN',
];
}
if ( $trainer ) {
foreach ($trainer as $value) {
$trainer_query[] = [
'key' => 'trainer',
'value' => $value,
'compare' => 'LIKE',
];
}
}
if ( $focus ) {
foreach ($focus as $value) {
$focus_query[] = [
'key' => 'body_focus',
'value' => $value,
'compare' => 'LIKE',
];
}
}
if ( $trainingtype ) {
foreach ($trainingtype as $value) {
$trainingtype_query[] = [
'key' => 'training_type',
'value' => $value,
'compare' => 'LIKE',
];
}
}
if ( $equipment ) {
foreach ($trainingtype as $value) {
$equipment_query[] = [
'key' => 'equipment',
'value' => $value,
'compare' => 'LIKE',
];
}
}
if ( $search ) {
$args['s'] = $search;
}
if($sort == 'newest'){
$args['orderby'] = [ 'date'=>'DESC' ];
}elseif($sort == 'olders'){
$args['orderby'] = [ 'date'=>'ASC' ];
}elseif($sort == 'shortest'){
$args['orderby'] = [ 'meta_value_num'=>'ASC' ];
$args['meta_key'] = 'duration';
}elseif($sort == 'longest'){
$args['orderby'] = [ 'meta_value_num'=>'DESC' ];
$args['meta_key'] = 'duration';
}
$args['meta_query']=array(
'relation' => 'AND',
array($min_lenght_query),
array($max_lenght_query),
array($minburn_query),
array($maxburn_query),
array($difficulty_query),
array('relation' => 'OR',$trainer_query),
array('relation' => 'OR',$focus_query),
array('relation' => 'OR',$trainingtype_query),
array('relation' => 'OR',$equipment_query),
);
【问题讨论】:
标签: php wordpress meta-query