Support

Account

Home Forums General Issues Two queries – one with checkbox ticked, one without – comparing meta_query

Helping

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

  • 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?

  • Hi @rdck

    Looking at the docs over at http://codex.wordpress.org/Class_Reference/WP_Query, your NOT LIKE code should work just fine.

    Perhaps you will need to find out the computed SQL to examine what is going wrong. There are a few plugins which will render out debug information such as SQL Queries.

    Thanks
    E

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Two queries – one with checkbox ticked, one without – comparing meta_query’ is closed to new replies.