WordPress умножает meta_query и один метаключ не может существовать

У меня проблема. У меня есть два метакейса price а также new-price, new-price не заполняет каждый пост И когда я делаю такой поиск, он работает для постов с обоими заполненными мета-ключами, но если new-price не заполнить это не.

Например, два продукта «Цена банана = 5, новая цена = 3» и «Цена Apple = 5». $startPrice=0 а также $endPrice=3, Это покажет мне и банан, и яблоко, но мне нужен только «Банан», потому что new-price вписывается в $startPrice а также $endPrice, Эта ошибка возникает из-за того, что «Apple» не заполнила мета-ключ «новая цена». Как это решить?

$clothes = get_posts(array(
'post_type' => 'clothes',
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'new-price',
'type' => 'numeric',
'value' => array($startPrice,$endPrice),
'compare' => 'between'
),
array(

'key' => 'price',
'type' => 'numeric',
'value' => array($startPrice,$endPrice),
'compare' => 'between'
)
)
)));

0

Решение

Решено так

$footwear = get_posts(array(
'post_type' => 'products_footwear',
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'main-razdel',
'value' => $categoryID,
'compare' => 'LIKE'
),
array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'new-price',
'value' => '',
'compare' => '!=',
'type' => 'NUMERIC'
),
array(
'key' => 'new-price',
'type' => 'numeric',
'value' => array($startPrice,$endPrice),
'compare' => 'between'
)
),
array(
'key' => 'price',
'type' => 'numeric',
'value' => array($startPrice,$endPrice),
'compare' => 'between'
)
)
)));
0

Другие решения

Есть еще один запрос, чтобы проверить, если ваш new-price пусто или нет.

попробуй это

$clothes = get_posts(
array(
'post_type' => 'clothes',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'new-price',
'value' => '',
'compare' => '!=',
'type' => 'NUMERIC'
),
array(
'relation' => 'OR',
array(
'key' => 'new-price',
'type' => 'numeric',
'value' => array($startPrice, $endPrice),
'compare' => 'between'
),
array(

'key' => 'price',
'type' => 'numeric',
'value' => array($startPrice, $endPrice),
'compare' => 'between'
)
),
),
)
);

это может сработать для вас. смотреть Вот для справки по использованию мета-запроса

0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector