Answer the question
In order to leave comments, you need to log in
WordPress: How to sort news by date from Custom Content?
There is a poster inform.kharkov.ua
There, entries are displayed in the order they were added to the site, and are not sorted by the date when this event occurs.
The date is specified via Custom Content.
How to make an output sorted by the date of the event, and not by adding it to the site?
Answer the question
In order to leave comments, you need to log in
You need to modify the request. If this is the main Loop - through the pre_get_posts hook , if this is a custom Loop, then directly in it, of course:
/**
* Custom loop с сортировкой по мета
*/
$upcoming_args = array (
'post_type' => 'event', // тип поста, если стандартный, то 'post'
'posts_per_page' => '20', // сколько записей на страницу
'order' => 'ASC',
'orderby' => 'meta_value', // сортировка по значению мета-поля
'meta_key' => 'date_from', // ключ (название) мета-поля
'meta_query' => array (
array (
'key' => 'date_from', // ключ (название) мета-поля
'value' => date('Ymd'), // текущая дата, в формате хранимого значения, от нее будем проверять
'compare' => '>=', // сравнение "больше или равно"
),
),
);
$upcoming = new WP_Query( $upcoming_args );
if ( $upcoming->have_posts() ) :
while ( $upcoming->have_posts() ) : $upcoming->the_post();
// Здесь выводим посты
endwhile;
endif;
wp_reset_postdata();
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question