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',                            

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


            $query = new WP_query($args);

Read more here: Sort posts by custom fields with empty values


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: