Как отсортировать по времени и категории?
генерирую посты с помощью аякса, категории у меня получилось вывести, а вот с датой проблема, дата в формате YYYYMMD, как сделать, чтобы отсортировать по выбранной дате, которая приходит, вот код
function pcht_posts($postID)
{
$catID = intval($_POST['cat_name']);
$timeProd = intval($_POST['time']);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $catID,
)
),
'meta_key' => 'get_date_sell',
'meta_type' => 'DATE',
);
$recent = new WP_Query($args);
if ($recent->have_posts()) {
while ($recent->have_posts()) : $recent->the_post();
wc_get_template_part('content', 'product');
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
}
Ответы (2 шт):
Автор решения: KAGG Design
→ Ссылка
Если в произвольном поле дата действительно хранится в виде текста YYYYMMDD, то должно работать простое добавление одной строки 'orderby' => 'meta_value' в $args:
$args = [
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'tax_query' => [
[
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $catID,
],
],
'meta_key' => 'get_date_sell',
'meta_type' => 'DATE',
'orderby' => 'meta_value',
];
Автор решения: Volodymyr
→ Ссылка
Делал буквально вчера
$args = array(
'post_per_page' => -1,
'post_type' => 'events',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'beginning_time',
'value' => array( date("Y-m-01"), date("Y-m-t") ),
'compare' => 'BETWEEN'
)
)
);
Думаю решение где-то рядом