How to get posts that have certain meta key in WordPress



 $post_type, 
        'posts_per_page' => -1, // sky is the limit
    );

    // We'll only list products whose locations are set on the map
    $args['meta_query'] = array(
        array(
            'key' => 'some_key',
            'value' => '',
            'compare' => '!='
        ),
    );

    $query = new WP_Query($args);
    
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();

            $rec['id'] = get_the_ID();
            $rec['title'] = get_the_title();
            $rec['permalink'] = get_permalink();
            $rec['thumbnail'] = get_the_post_thumbnail($rec['id'], 'thumbnail' /* , 'full' */);
            $rec['thumbnail'] = empty($rec['thumbnail']) ? '/some/default/image.png' : $rec['thumbnail'];

            $items[$rec['id']] = $rec;
        }
    }

    wp_reset_postdata();

    return $items;
}

?>

Disclaimer: The content in this post is for educational purposes only. Always remember to take a backup before doing any of the suggested steps just to be on the safe side.
Referral Note: When you purchase through a referral link (if any) on this page, we may earn a commission.
If you're feeling thankful, you can buy me a coffee or a beer