最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Sort by two dates. Default entry date and custom field if present

programmeradmin2浏览0评论

I have a custom post type where there is an option to sort by date. There is however a custom date field that allows changing the display date. This custom date field is not always filled.

Right now I'm trying to sort by date, and if the custom date field is present use that field when sorting instead.

I have the following that doesn't really work since I believe orderby sorts by the custom field and then tacks on sorted entry dates.

$args = array( 
    'post_type' => array('post'),
    'order'     => 'DESC',
    'orderby'   => array('upcoming_class_date' => 'DESC', 'date' => 'DESC'),
    'category_name'   => 'upcoming class',
    'meta_key'      => 'company',
    'meta_value'    => $company,            
    'showposts' => 40
);

$loop = new WP_Query( $args );

I was thinking of grabbing the results and resorting them after query, but that seems very inefficient.

I have a custom post type where there is an option to sort by date. There is however a custom date field that allows changing the display date. This custom date field is not always filled.

Right now I'm trying to sort by date, and if the custom date field is present use that field when sorting instead.

I have the following that doesn't really work since I believe orderby sorts by the custom field and then tacks on sorted entry dates.

$args = array( 
    'post_type' => array('post'),
    'order'     => 'DESC',
    'orderby'   => array('upcoming_class_date' => 'DESC', 'date' => 'DESC'),
    'category_name'   => 'upcoming class',
    'meta_key'      => 'company',
    'meta_value'    => $company,            
    'showposts' => 40
);

$loop = new WP_Query( $args );

I was thinking of grabbing the results and resorting them after query, but that seems very inefficient.

Share Improve this question edited Apr 22, 2019 at 19:19 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Apr 22, 2019 at 18:38 ChrisChris 1255 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Few issues I see here with your code:

  1. You have category_name set to upcoming class ... category slugs do not have spaces in them, so this is incorrect. If you meant to have all upcoming and class categories, then use upcoming,class https://developer.wordpress/reference/classes/wp_query/#category-parameters

  2. You have a meta query on company but it sounds like you're also trying to sort by a custom field upcoming_class_date, and as such, this field also needs to be included in your query. You will need to change this to an advanced meta query to handle this. https://developer.wordpress/reference/classes/wp_query/#custom-field-post-meta-parameters

  3. You said a custom post type -- but your code has post as the post type

This is some quick example code I came up with on how you should setup your query arguments:

$args = array(
    'post_type'     => array( 'post' ),
    'order'         => 'DESC',
    'orderby'       => array( 'upcoming_class_date' => 'DESC', 'date' => 'DESC' ),
    'category_name' => 'upcoming class',
    'showposts'     => 40,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key'     => 'company',
            'value'   => $company,
            'compare' => '=',
        ),
        array(
            'relation' => 'OR',
            array(
                'key'     => 'upcoming_class_date',
                'compare' => 'EXISTS',
            ),
            array(
                'key'     => 'upcoming_class_date',
                'compare' => 'NOT EXISTS',
            )
        )

    ),
);

You will notice there is an EXISTS and NOT EXISTS .. this is because you said that sometimes a post will not have a value for that field -- to make sure the value is included in the query, we have to add both of those and set the relation to OR (that way it returns posts with that field and ones without).

The other potential issue I see is the format the date is saved in -- which could cause sorting problems, but you did not provide details on the format.

I recommend reading this answer on a similar question to better understand how WP_Query works with meta queries: https://wordpress.stackexchange/a/285012/51201

You can also do an orderby using the meta query array key (but they have to be changed to associative array) -- there are examples in the link above for that.

发布评论

评论列表(0)

  1. 暂无评论