1

So, I have a custom content type called "team" where I have an ACF field attached called "external" which is a checkbox. I am currently getting all the posts by passing the following arguments array to the get_post function.

 $args = array(
            'posts_per_page'   => -1,
            'offset'           => 0,
            'orderby' => 'menu_order', 
            'order' => 'ASC', 
            'post_type'        => 'team',
            'post_status'      => 'publish',

        );

What I'd like to do is run this in two functions checking if the field is or is not checked, essentially filtering by who is and isn't internal. I'm not sure how to set the meta-query up for this.

1

2 Answers 2

0

So, the scattered documentation on this is really less than clear.

For checking for unchecked posts added the following meta_query:

    'meta_query' => array(array(
    'key' =>  'external',
    'value'  => '0',
    'compare' =>  '='
    ))

For filtering by checked it was just this:

     meta_query => array(array(
    'key' =>  'external',
    'value'  => '0',
    'compare' =>  '='
    ))
0

You can achieve this by using the meta_query parameter in the WP_Query arguments to filter posts based on the value of the ACF checkbox field. Set to 0 for unchecked and Set to 1 for checked in value parameter in meta_query.

// Get internal team members
$args_internal = array(
    'posts_per_page'   => -1,
    'orderby'          => 'menu_order',
    'order'            => 'ASC',
    'post_type'        => 'team',
    'post_status'      => 'publish',
    'meta_query'       => array(
        array(
            'key'     => 'external',   // ACF field key or name
            'value'   => 0,             // Set to 0 for unchecked
            'compare' => '=',
            'type'    => 'NUMERIC',     // Adjust the type based on your field type
        ),
    ),
);

$query_internal = new WP_Query( $args_internal );

// Get external team members
$args_external = array(
    'posts_per_page'   => -1,
    'orderby'          => 'menu_order',
    'order'            => 'ASC',
    'post_type'        => 'team',
    'post_status'      => 'publish',
    'meta_query'       => array(
        array(
            'key'     => 'external',   // ACF field key or name
            'value'   => 1,             // Set to 1 for checked
            'compare' => '=',
            'type'    => 'NUMERIC',     // Adjust the type based on your field type
        ),
    ),
);

$query_external = new WP_Query( $args_external );

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.