I'm trying to get posts that all have a custom field named webinar, but I just get all posts. Here is what I have:

    $args = array(
        'showposts' => -1,
        'post_type' => 'post',
        'meta_query' => array(
            'key'     => 'webinar',
            'compare' => 'EXIST',

    $loop = new WP_Query($args);
    if($loop->have_posts()) {
        while ( $loop->have_posts() ) {
            $output .= get_the_title();


1 Answer 1


That's not how meta_query works, the documentation at WordPress.org states that meta_query does not take parameters, but rather arrays of parameters

meta_query also contains one or more arrays with the following keys:

Then further down:

Important Note: meta_query takes an array of meta query arguments arrays (it takes an array of arrays) – you can see this in the examples below.

This construct allows you to query multiple metadatas by using the relation parameter in the first (outer) array to describe the boolean relationship between the meta queries. Accepted arguments are ‘AND’, ‘OR’. The default is ‘AND’.


(Note that meta_query expects nested arrays, even if you only have one query.)

So this:

        'meta_query' => [
            'key'     => 'webinar',
            'compare' => 'EXIST',

Should be this:

        'meta_query' => [
                'key'     => 'webinar',
                'compare' => 'EXIST',

However, this entire approach is wrong, and the query is going to be super slow/expensive, it does not have to be this way. Custom fields are not designed for finding posts, searching for, filtering for, querying for posts by their post meta values is super expensive/heavy/slow/unnecessary.

There are 2 alternatives that could be thousands of times faster, and easier, and they allow you to avoid having to build a custom query completely ( and they give you free pagination, REST APIs, URLs, admin screens, etc )

1. Custom Post Types, you could have created a Webinar post type. Then you'd have a webinar archive to at /webinar which could be changed via the rewrite parameter, archive-webinar.php in your theme, all magically auto-created for free! There would even be a separate area of the WP Admin interface for webinars. Why go to all that effort recreating it with a custom field, save yourself the hassle. You can go to a site like https://generatewp.com/post-type/ or one of their competitors, and fill in some questions and it'll generate the code for you to copy paste

2: Custom Taxonomy, if you ever need to filter/search/query/restrict/etc then use a custom taxonomy, that's what they were designed for. There's a reason tags and categories aren't stored as custom fields. These give you a lot of free stuff too. Like free templates taxonomy-webinar.php in your theme, free archive URLs, user interfaces, REST API for your javascript, etc. You can even generate the code for free with the same website as the custom post type and copy paste

Further notes:

  • Don't bunch up all your output in a pointless variable, it just means your browser is sitting their waiting for output, and it makes escaping super hard. If you need it as a string for a shortcode, just use an output buffer
  • Clean up after the query and call wp_reset_postdata just after the while loop ends, or the current post will be set to the last post of the loop rather than the current page
  • Avoid 'showposts' => -1, and posts_per_page set to -1, set it to a super high number you know it will never reach, but never -1, otherwise if there's an accident, if business changes, if the site adds webinars over the years, you can get memory exhaustion or timeout errors. Don't say it will never happen, instead set a high number and guarantee it can't happen
  • 1
    Thank you, Tom. I don't need a custom post type at the moment, but I did create a custom taxonomy called webinars and was able to get it working the way I wanted. Thanks for the quick direction.
    – Michael
    Sep 10, 2020 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.