Two queries – one with checkbox ticked, one without – comparing meta_query

enter image description here

I am trying to do the following:

Two queries
1. Get all people that have director ‘yes’ ticked
2. Get all people that don’t have director ‘yes’ ticked

I have the first query all set up and works well (‘people’ is a CPT, and director is an ACF of a checkbox ‘yes’).

<?php $directors = get_posts(array(
    'post_type' => 'people',
    'orderby' => 'meta_value',
    'meta_key' => 'last_name',
    'order' => 'ASC',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
        'key' => 'director',
        'value' => '"yes"',
        'compare' => 'LIKE'
        )
    )
)); ?>

What this query does it get all ‘people’, ordered by their last name, and, using meta_query gets all those who have the directors checkbox ticked ‘yes’.

So, my issue is that now what I want to do, for the second query, is get all those people where ‘director’ ‘yes’ isn’t ticked.

Here is what I have so far:

<?php $people = get_posts(array(
    'post_type' => 'people',
    'orderby' => 'meta_value',
    'meta_key' => 'last_name',
    'order' => 'ASC',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'company',
        'value' => '"' . get_the_ID() . '"',
        'compare' => 'LIKE'
        ),
        array(
        'key' => 'director',
        'value' => 'yes',
        'compare' => 'NOT LIKE'
        )
    )
)); ?>

I have tried a few other compare arguments, but can’t seem to get it to work.

Any help or ideas?

Read more here: Two queries – one with checkbox ticked, one without – comparing meta_query

Leave a Reply

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