L
L
Lici2014-07-13 18:46:41
CMS
Lici, 2014-07-13 18:46:41

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

3 answer(s)
R
Ruslan, 2014-07-13
@rOOse

'orderby' => 'meta_value'

A
Alexander Zelenin, 2014-07-14
@zelenin

codex.wordpress.org/Class_Reference/WP_Query#Order...

I
Igor Vorotnev, 2014-10-05
@HeadOnFire

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 question

Ask a Question

731 491 924 answers to any question