Home > Blockchain >  WordPress Query order by two keys in dependency
WordPress Query order by two keys in dependency

Time:01-29

I sort a query by a customfield start_date (form type Ymd). The sorting should always be descending from new to old. If there are multiple entries for the date on one day, the entries should be sorted by the order of publication.

I tried it with the (post) date, ID, menu_order but it never works in all cases.

The start_date may be a different day than the post date.

Is there any query way?
Or do I have to add hours and minutes to the news_start_date field?
Or maybe switch to the post date over all, but this means in this backend flow a big lost of elegance.

$meta_query = array(
    'relation' => 'AND',
    array(
        'key'     => 'news_start_date',
        'value'   => $today,
        'compare' => '<=',
        'type'    => 'DATE'
    ),
    array(
         'relation' => 'OR', 
         
        array ( 
            'relation' => 'AND', 
                array(
                    'key'     => 'news_start_date',
                    'value'   => $expire_date,
                    'compare' => '>=',
                    'type'    => 'DATE'
                ),
                array(
                    'key'     => 'news_end_date',
                    'compare' => 'NOT EXISTS'
                 ),      
        ),  
         
        array ( 
            'relation' => 'AND', 
            array(
                'key'     => 'news_end_date',
                'compare' => 'EXISTS'
             ), 
            array(
                'key'     => 'news_end_date', 
                'value'   => $today,
                'compare' => '>=',
                'type'    => 'DATE'
            ),           
        ),  
    ),            
);
$args = array(
    'post_type' => 'news',
    'meta_query' => $meta_query, 
    'posts_per_page' => $news_count,
    'meta_key' => 'news_start_date',
    'orderby' => 'meta_value',
    'orderby' => array(
        'meta_value' => 'DESC',     
        'ID' => 'DESC',
        //'menu_order' => 'DESC',
        //'date' => 'DESC',
        //'publish_date' => 'DESC',
    ),
    'order' => 'DESC', 
    'post__not_in' => $sticky_posts, 
    'ignore_sticky_posts' => true,
    'cache_results' => true, 
    'update_post_meta_cache' => true, 
    'no_found_rows' => true, 
    'suppress_filters' => false,
 );

I tried the others fields but there was never a 100% correct order

CodePudding user response:

Add a second orderby parameter to your query args, where the first orderby is meta_value, and the second orderby is ID. This will sort the results by the news_start_date field first, and then sort by the ID field (which represents the order of publication) as a secondary sorting option.

You can do this by adding this line to your query args:

'orderby' => array(
'meta_value' => 'DESC',
'ID' => 'DESC',
),

The query results will be sorted by 'news_start_date' in descending order first, and then by the 'ID' in descending order, this will give you the results in the order you want.

  • Related