• Here is my function to sort the posts or in this case ‘games’ by a cutsom field called “score’.

    function my_modify_main_query($query)
    {
        if($query->is_archive() && $query->is_main_query()){
            $query->set('post_type', array('game', 'post'));
            $query->set('meta_key', 'score');
            $query->set('orderby', 'meta_value_num');
            $query->set('order', 'DESC');
        }
    }
    
    add_action('pre_get_posts', 'my_modify_main_query');

    It works but this query excludes all posts without this custom field, too.

    How can I include them without setting an custom field with just ‘0’? The query should assume that there is a custom field called “score” with a value of 0.

    Or in other words:
    Normally there a 100+ games there. Only a few of them have a “score”. I want to sort the games with the hightes score first (works) and include the other games without a score after that. Any idea how to accomplish that? Thank you!

The topic ‘$query for 'meta_key' excludes items without that custom field. Why?’ is closed to new replies.