Returning a list of custom post types excluding those without a specific meta_value

I technically have three questions for this, but they’re related and require the first to be answered, so I hope it’s okay to post all here.

To put this in a little context, I’m working on a directory of sorts where I can add to 3 URLs in 3 meta fields for each post. I want to display a list of only posts that have 1-3 of said URLs added. To make this extra interesting, I also have 3 corresponding checkboxes to each meta field that I can select in case an item is sold out. I would like to exclude sold out items from this list as well.

Question 1 – How do I display a list of custom post type posts that all have the same meta_value and key (excluding those that have the same value)

I’ve managed to make a list that returns all myPostType posts where 'meta_value' => false in myMetaKey. See code below:

    $args = array(
        'post_type' => 'myPostType', 
        'meta_key' => 'myMetaKey', 
        'meta_value' => false, 
        'orderby' => '_custom', 
        'order' => 'ASC', 
        'posts_per_page' => -1, );
    $loop = new WP_Query($args);
    while ($loop->have_posts() ) : $loop->the_post();

I am interested in having a list that returns posts that does have a meta_value in myMetaKey. I thought it would be as simple as to change false to true in meta_value, but that returns an empty list instead.

Question 2 – How do I return a list that includes posts with different meta_key‘s?

I’m interested in also having the list display posts from myPostType that have both myMetaKeyA, myMetaKeyB and myMetaKeyC – but only if meta_value is true (or the equivalent as asked above in questions 1).

Question 3 – How do I exclude posts that have a meta_key with a corresponding checkbox selected

As mentioned in the beginning I’m also interested in excluding posts via a selected checkbox.

Read more here: Returning a list of custom post types excluding those without a specific meta_value

Leave a Reply

Your email address will not be published. Required fields are marked *