【发布时间】:2019-07-27 06:18:18
【问题描述】:
我正在创建一个前端过滤器,其中包含访问者可以单击以根据分类对帖子进行排序的复选框。他们可以过滤的帖子是自定义帖子类型,附加了 3 个分类法。
这是一个自定义的 ajax 实时过滤器,因此每当有人单击复选框时,都会返回结果,没有页面加载,也没有提交按钮。
前端工作正常,发送到端点的信息也很好,如果我将查询缩小到只有一个分类,一切都会按预期工作,不管分类我缩小过滤器。但是由于查询中有多种分类法,我遇到了问题。这是我第一次创建这样的东西,所以我希望这只是我没有想到的一件小事,但无论我尝试什么,分类过滤器只过滤具有术语的第一个分类检查。
一个例子:
税 1 税 2 税 3
如果被点击的第一个复选框属于 Tax 1,那么它只会在 Tax 1 内过滤。如果我尝试单击属于 Tax 2 或 Tax 3 的复选框,它会完全忽略这些条款/税,并且仍然只按第一个分类法过滤。
我知道我在查询中做错了什么,所以我真的希望 SO 上的某个人能给我一些指导,让我知道我应该怎么做才能让它工作。
这里是端点(查询):
function myFilter ($data) {
$checkFylker = $data['checkFylker'];
$checkUtstyr = $data['checkUtstyr'];
$checkFors = $data['checkFors'];
$checkType = $data['checkType'];
//return $results;
//Main $args
$args = array(
'post_type' => 'ml_opp', // Query only "ml_opp" custom posts
'post_status' => 'publish', // Query only posts with "publish" status
'orderby' => 'date', // Sort posts by date
'order' => 'ASC' // ASC
);
$args['tax_query'] = array( 'relation'=>'AND' ); // AND means that all conditions of meta_query should be true
// for taxonomies / utstyr
if( isset( $checkUtstyr ) )
$args['tax_query'][] = array(
array(
'taxonomy' => 'ml_utstyr',
'field' => 'id',
'terms' => $checkUtstyr
)
);
//for taxonomies / forsendelse
if( isset( $checkFors ) )
$args['tax_query'][] = array(
array(
'taxonomy' => 'ml_forsendelse',
'field' => 'id',
'terms' => $checkFors
)
);
// for taxonomies / fylker
if( isset( $checkFylker ) )
$args['tax_query'] = array(
array(
'taxonomy' => 'ml_fylk',
'field' => 'id',
'terms' => $checkFylker
)
);
// for taxonomies / type
if( isset( $checkType ) )
$args['tax_query'] = array(
array(
'taxonomy' => 'ml_typ',
'field' => 'id',
'terms' => $checkType
)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
echo '<h2>' . $query->post->post_title . '</h2>';
endwhile;
wp_reset_postdata();
else :
echo 'No posts found';
endif;
die();
}
【问题讨论】:
标签: php ajax wordpress filtering