• I’m trying to write a WP_Query which uses some data from two ACF checkboxes as part of the arguments.

    I found the documentation showing how to use fields in custom queries however I cannot work out what the correct syntax for my checkboxes is.

    My ACF’s:

    • Label: Promote to homepage?
    • Name: promote_to_homepage
    • Choices: Promote to homepage : Promote to homepage
    • Label: Make feature?
    • Name: make_feature
    • Choices: Show as feature : Show as feature (top of homepage)

    This is the query I have:

    $the_query = new WP_Query(
       array
          (
             'posts_per_page' => 1,
             'meta_key' => 'promote_to_homepage',
             'meta_value' => 'Promote to homepage',
             'meta_key' => 'make_feature',
             'meta_value' => 'Make feature'
          )
    );

    And I’ve tried using true instead:

    array
       (
          'posts_per_page' => 1,
          'meta_key' => 'promote_to_homepage',
          'meta_value' => true,
          'meta_key' => 'make_feature',
          'meta_value' => true
    )

    And also this:

    $args = array(
            'post_type' => 'post',
            'posts_per_page' => 1,
            'meta_query' => array(
            'relation' => 'AND',
                array(
                    'key' => 'promote_to_homepage',
                    'value' => true,
                ),
                array(
                    'key' => 'make_feature',
                    'value' => true,
                ),
            )
         );
    
        $the_query = new WP_Query($args);

    I guess what I can’t figure out is why data is actually needed for meta_key and meta_value. Is the key the label? Is the value one of the choices? I have tried many variations and cannot get it to work.

    Essentially what I want to do is output the most recent post that is checked for “Promote to homepage” and “Make feature”.

The topic ‘[Plugin: Advanced Custom Fields] Using checkboxes in queries’ is closed to new replies.