As stated in @ambroseya’s answer, its supposed to work like that. Once you declare a meta query, even if you aren’t looking for a specific value, it will only query posts with that meta key declared. If you want to include all posts sort them by the meta key, use the following code:
$args = array(
'post_type' => 'news',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key'=>'custom_author_name',
'compare' => 'EXISTS'
),
array(
'key'=>'custom_author_name',
'compare' => 'NOT EXISTS'
)
),
'posts_per_page'=>-1
);
$query = new WP_Query($args);
echo $query->found_posts;
What this does is use an advanced meta query that looks for posts that do and don’t have that meta key declared. Since the one with EXISTS
is first, when you sort by meta_value
, it’ll use the first query.