我试图在wordpress中制作OR条件。但它没有对WP_Query($ args)的查询产生任何影响我对wordpress很新,所以我对wordpress查询的流程一无所知。
if ( $params ) {
extract($params);
$args['meta_query'] = array(
'relation' => 'OR'
);
if (isset( $keywords )) {
$args['meta_query'][] = array(
'meta_key' => 'eltd_listing_address',
'meta_value' => $keywords,
);
}
if (isset($type)) {
if($type !== '' && $type !=='all' ){
$args['meta_query'][] = array(
'meta_key' => 'eltd_listing_item_type',
'meta_value' => $type,
);
}
}
if (isset($number)) {
$args['posts_per_page'] = $number;
}
$args['tax_query'] = array(
'relation' => 'AND'
);
if ( isset( $category ) ) {
if($category !== '' && $category !=='all' ){
$args['tax_query'][] = array(
'taxonomy' => 'listing-item-category',
'field' => 'term_id',
'terms' => (int)$category
);
}
}
if ( isset($location) ) {
if($location !== '' && $location !=='all' ){
$args['tax_query'][] = array(
'taxonomy' => 'listing-item-location',
'field' => 'term_id',
'terms' => (int)$location
);
}
}
if ( isset($tag) ) {
$args['tax_query'][] = array(
'taxonomy' => 'listing-item-tag',
'field' => 'term_id',
'terms' => (int)$tag
);
}
}
$query = new WP_Query($args);
当我在$ query->中显示请求时,它似乎就是这样。
在$ args中我得到了
Array
(
[post_type] => listing-item
[post_status] => publish
[meta_query] => Array
(
[relation] => OR
[0] => Array
(
[meta_key] => eltd_listing_address
[meta_value] => Carrer de la Princesa, 24-26, 08003 Barcelona, Spain
)
[1] => Array
(
[meta_key] => eltd_listing_item_type
[meta_value] => 277
)
)
[posts_per_page] => 12
)
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'listing-item' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 12
这里我没有得到OR条件。
我不知道如何在wordpress中做。任何解决方案将高度赞赏。
答案 0 :(得分:0)
您需要为每个键设置元查询的比较参数...所以
if (isset( $keywords )) {
$args['meta_query'][] = array(
'meta_key' => 'eltd_listing_address',
'meta_value' => $keywords,
);
}
if (isset($type)) {
if($type !== '' && $type !=='all' ){
$args['meta_query'][] = array(
'meta_key' => 'eltd_listing_item_type',
'meta_value' => $type,
);
}
}
变为
if (isset( $keywords )) {
$args['meta_query'][] = array(
'meta_key' => 'eltd_listing_address',
'meta_value' => $keywords,
'compare' => '=',
);
}
if (isset($type)) {
if($type !== '' && $type !=='all' ){
$args['meta_query'][] = array(
'meta_key' => 'eltd_listing_item_type',
'meta_value' => $type,
'compare' => '=',
);
}
}