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

wp query - How do I "orderby" a column without losing posts that don't have the column?

programmeradmin1浏览0评论

I'm currently using the following code to sort and order my super awesome column that I created:

        if ( 'My column' !== $query->get( 'orderby' ) ) {
            return;
        }

        // Order by the _my_column using a numeric interpretation of the value.
        $query->set( 'meta_key', '_my_column' );
        $query->set( 'orderby', 'meta_value_num' );

This code gets the job done, but when I click on the column header in order to sort the column, any posts that don't contain that meta field are not included in the listing of posts. I would like to have it such that any posts that don't contain the meta field evaluate as and are sorted as zeroes. How can I accomplish this? Thanks.

I'm currently using the following code to sort and order my super awesome column that I created:

        if ( 'My column' !== $query->get( 'orderby' ) ) {
            return;
        }

        // Order by the _my_column using a numeric interpretation of the value.
        $query->set( 'meta_key', '_my_column' );
        $query->set( 'orderby', 'meta_value_num' );

This code gets the job done, but when I click on the column header in order to sort the column, any posts that don't contain that meta field are not included in the listing of posts. I would like to have it such that any posts that don't contain the meta field evaluate as and are sorted as zeroes. How can I accomplish this? Thanks.

Share Improve this question asked Aug 21, 2020 at 22:17 Nicholas CardotNicholas Cardot 1851 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You can do it like so:

  1. Use the meta_query parameter to query both posts that have the metadata _my_column (or whatever is the meta key) and posts that do not have it (i.e. does not exist in the database).

  2. Use a custom name (i.e. array key) with the above meta query clauses and then use the name in the orderby parameter.

See Query improvements in WP 4.2: ‘orderby’ and ‘meta_query’ for further details.

So instead of using the meta_key parameter:

Note: I see you used meta_value_num, so I used the 'type' => 'NUMERIC' to make sure the meta value is treated as a (signed) integer.

  • If you don't want/need to keep existing meta queries (in the $query object):

    $query->set( 'meta_query', array(
        'relation'             => 'OR',
    
        // Clause 1, named my_column_exists:
        // Query posts that do have the metadata _my_column.
        'my_column_exists'     => array(
            'key'     => '_my_column', // meta key
            'compare' => 'EXISTS',
            'type'    => 'NUMERIC',
        ),
    
        // Clause 2, named my_column_not_exists:
        // OR that do NOT have the metadata.
        'my_column_not_exists' => array(
            'key'     => '_my_column', // meta key
            'compare' => 'NOT EXISTS',
            'type'    => 'NUMERIC',
        ),
    ) );
    
    $query->set( 'orderby', array(
        // Sort by the _my_column metadata first.
        'my_column_not_exists' => 'DESC',
    
        // Then if you want, by the post date, title, etc.
        'date'                 => 'ASC',
    ) );
    
  • Otherwise (to keep existing meta queries), you can do something like:

    $query->set( 'meta_query', array(
        // Note: Here the 'relation' defaults to AND.
    
        // Clause 1, unnamed.
        array(
            'relation'             => 'OR',
    
            // Sub-clause 1, named my_column_exists:
            // Query posts that do have the metadata _my_column.
            'my_column_exists'     => array(
                'key'     => '_my_column', // meta key
                'compare' => 'EXISTS',
                'type'    => 'NUMERIC',
            ),
    
            // Sub-clause 2, named my_column_not_exists:
            // OR that do NOT have the metadata.
            'my_column_not_exists' => array(
                'key'     => '_my_column', // meta key
                'compare' => 'NOT EXISTS',
                'type'    => 'NUMERIC',
            ),
        ),
    
        // Clause 2, unnamed.
        // Include the existing meta queries.
        (array) $query->get( 'meta_query' ),
    ) );
    
    $query->set( 'orderby', array(
        // Sort by the _my_column metadata first.
        'my_column_not_exists' => 'DESC',
    
        // Then if you want, by the post date, title, etc.
        'date'                 => 'ASC',
    ) );
    
发布评论

评论列表(0)

  1. 暂无评论