Support

Account

Home Forums General Issues Using checkbox fields in custom queries

Solving

Using checkbox fields in custom queries

  • 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'
          )
    );

    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? Or do I need to use meta_value => true or something? 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 ticked true/yes for “Promote to homepage” and “Make feature”.

    Thanks!

    Edit: I have also tried:

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

    And I’ve tried:

    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,
        ),
        )
    );

    Have now also tried:

    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);
  • Are you using a checkbox field or a true/false field.

    Checkboxes are stored as serialized data and you’re not going to be able to use WP_Query to filter by a checkbox field.

    If you use a true/false field then you can use WP_Query, the values of a true/false field are 0 (zero) for false and 1 for true.

    the last code example you have should work for a true/false field if use substitute 0 & 1 where you have true and false.

  • If the checkbox is not checked, there is no value, but also no key saved. So the whole custom field does not exist in the database.

    What you should do with true/false or checkboxes is to check if the value of field exists in the database.

    You can easily do this by using the “EXISTS” and “NOT EXISTS” compare operators.

    
    'meta_query'    => array(
        array(
            'key'	  	=> 'people_hide_on_archive',
            'value'	  	=> 'true',
            'compare' 	=> 'NOT EXISTS',
        ),
    )
    

    Hope this helps you.

  • I had a similar problem where the checkbox had been checked, creating the key, and then being unchecked at a later time. I seemed to have fixed it with this (‘rented’ is the checkbox field):

    'meta_query' => array(
      'relation' => 'AND',
      array(
        'key' => 'status',
        'value' => 'active',
      ),
      array(
        'relation' => 'OR',
        array(
          'key' => 'rented',
          'value' => '1',
          'compare' => '!='
        ),
        array(
          'key' => 'rented',
          'value' => '1',
          'compare' => 'NOT EXISTS'
        )
      )
    )
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Using checkbox fields in custom queries’ is closed to new replies.