Support

Account

Home Forums General Issues Need query help for multiple checkbox values

Helping

Need query help for multiple checkbox values

  • Hi
    I have added a very simple custom field to a post – checkbox with multiple colors. I am trying to select all posts that have both colors red and orange checked. I can select a single color as shown below (red). When I change the query to include both colors I get zero results. Please help – what is wrong? This is just an example. It’s not working in my project either.
    – Thank you

    There are 2 red posts and 1 orange and I can select them separately.

    this is what doesn’t work:
    $meta_query_args = [
    [
    ‘key’ => ‘color’,
    ‘value’ => array(‘red’,’orange’),
    ‘compare’ => ‘IN’,
    ],
    ];

    The code below does work:

    wp_reset_postdata();
    $meta_query_args = [
    [
    ‘key’ => ‘color’,
    ‘value’ => ‘red’,
    ‘compare’ => ‘LIKE’,
    ],
    ];
    $args = [
    ‘post_type’ => ‘post’,
    ‘posts_per_page’ => 100, // Set this to a reasonable limit
    ‘meta_query’ => $meta_query_args,

    ];

    $the_query = new WP_Query( $args );
    while ( $the_query->have_posts() ) : $the_query->the_post();
    $post_id = get_the_ID();

    setup_postdata( $post );
    //echo “post id:”.$post->ID;
    $title = get_the_title();

    $color = get_field(‘color’);

    echo “<h3>” . $title . $color[0] . $color[1] . $color[2] . “</h3>”;

    endwhile;

  • 
    $meta_query_args = [
      'relation' => 'AND',
      [
        'key' => 'color,
        'value' => '"red"',
        'compare' => 'LIKE',
      ],
      [
        'key' => 'color,
        'value' => '"orange"',
        'compare' => 'LIKE',
      ],
    ];
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Need query help for multiple checkbox values’ is closed to new replies.