Sort posts by custom fields with empty values

admin2025-01-08  5

I'm trying to list a few post sorting by a custom field value ("space-comments"). It's working very well, however, when the custom field is empty the loop don't get this post.

Any idea to fix it? Thanks :)

            $args = array( 
                'post_type' => 'spaces',
                'post_per_page' => '500',
                'meta_key' => 'space-comments',                 
                'orderby' => 'meta_value_num',                  
                'order' => 'DESC',
                'author__in' => $tradicionalIds,
                    'meta_query' => array(
                        'relation' => 'AND',                            

                        array(
                            'key' => 'space-city',
                            'value' => $search,
                            'compare' => 'LIKE'
                       ),
                        array(
                            'key' => 'space-comments',
                            'compare' => 'EXISTS',                              
                        ),          

                    )
                );



            $query = new WP_query($args);

I'm trying to list a few post sorting by a custom field value ("space-comments"). It's working very well, however, when the custom field is empty the loop don't get this post.

Any idea to fix it? Thanks :)

            $args = array( 
                'post_type' => 'spaces',
                'post_per_page' => '500',
                'meta_key' => 'space-comments',                 
                'orderby' => 'meta_value_num',                  
                'order' => 'DESC',
                'author__in' => $tradicionalIds,
                    'meta_query' => array(
                        'relation' => 'AND',                            

                        array(
                            'key' => 'space-city',
                            'value' => $search,
                            'compare' => 'LIKE'
                       ),
                        array(
                            'key' => 'space-comments',
                            'compare' => 'EXISTS',                              
                        ),          

                    )
                );



            $query = new WP_query($args);
Share Improve this question asked May 23, 2017 at 19:53 Pedro SantosPedro Santos 134 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

I found an other post on stackexchange that seems to answer your question:

Order by optional meta key?

To sort posts by custom field values, including posts with empty custom field values, you need to adjust your meta_query parameter. Instead of using 'compare' => 'EXISTS', you should use 'compare' => 'EXISTS' for posts that have a value for the custom field and another meta_query array with 'compare' => 'NOT EXISTS' for posts with empty values

$args = array( 
    'post_type' => 'spaces',
    'posts_per_page' => 500, // It should be 'posts_per_page' not 'post_per_page'
    'meta_key' => 'space-comments',                 
    'orderby' => 'meta_value_num',                  
    'order' => 'DESC',
    'author__in' => $tradicionalIds,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'space-city',
            'value' => $search,
            'compare' => 'LIKE'
        ),
        array(
            'relation' => 'OR',
            array(
                'key' => 'space-comments',
                'compare' => 'EXISTS'
            ),
            array(
                'key' => 'space-comments',
                'compare' => 'NOT EXISTS',
                'value' => ''
            )
        )
    )
);

$query = new WP_Query($args);

please modified your query will retrieve posts with non-empty custom field values for space-comments as well as posts with empty values for space-comments

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265636a1058.html

最新回复(0)