Support

Account

Home Forums General Issues get select value in meta query

Solving

get select value in meta query

  • hi everyone,

    i want to get my post with custom query, but when i filter posts with the meta value that has an array, my posts doesn’t show anymore.

    $args = array(
      'post_type'      => 'cafe',
      'posts_per_page' => 12,
      'rewrite' => array( 'slug' => 'cafe', 'with_front' => true),
      'meta_query' => array(
        array(
          'key'     => 'cafe_state',
          'value'   => $cafe_state,
          'compare' => 'LIKE',
        ),
        array(
          'key'     => 'cafe_area',
          'value'   => $cafe_area,
          'compare' => 'LIKE',
        ),
        array(
          'key'     => 'cafe_name',
          'value'   => $cafe_name,
          'compare' => 'LIKE',
        ),
        array(
          'key'     => 'cafe_facilities',
          'value'   => array( 'test' ),
          'compare' => 'IN',
        ),
      ),
    );
    
    $query = new wp_query( $args );
  • I’m guessing that you mean the cafe_facilities field? Is this a checkbox field or some other type of field that has multiple values?

  • @hube2 it’s a checkbox field, return multiple values

  • Querying a checkbox field is similar to querying a relationship field because it stores a serialized array https://www.advancedcustomfields.com/resources/querying-relationship-fields/

    
    $args = array(
      'post_type'      => 'cafe',
      'posts_per_page' => 12,
      'rewrite' => array( 'slug' => 'cafe', 'with_front' => true),
      'meta_query' => array(
        array(
          'key'     => 'cafe_state',
          'value'   => $cafe_state,
          'compare' => 'LIKE',
        ),
        array(
          'key'     => 'cafe_area',
          'value'   => $cafe_area,
          'compare' => 'LIKE',
        ),
        array(
          'key'     => 'cafe_name',
          'value'   => $cafe_name,
          'compare' => 'LIKE',
        ),
        array(
          'key'     => 'cafe_facilities',
          'value'   => '"test"',
          'compare' => 'LIKE',
        ),
      ),
    );
    
  • @hube2 how can i select multiple value?

    for example: ‘test’, ‘tests’

  • That depends on if you want to get posts with both values or with either value (AND/OR).

    
    // AND
    $args = array(
      'post_type'      => 'cafe',
      'posts_per_page' => 12,
      'rewrite' => array( 'slug' => 'cafe', 'with_front' => true),
      'meta_query' => array(
        array(
          'key'     => 'cafe_state',
          'value'   => $cafe_state,
          'compare' => 'LIKE',
        ),
        array(
          'key'     => 'cafe_area',
          'value'   => $cafe_area,
          'compare' => 'LIKE',
        ),
        array(
          'key'     => 'cafe_name',
          'value'   => $cafe_name,
          'compare' => 'LIKE',
        ),
        array(
          'key'     => 'cafe_facilities',
          'value'   => '"test"',
          'compare' => 'LIKE',
        ),
        array(
          'key'     => 'cafe_facilities',
          'value'   => '"tests"',
          'compare' => 'LIKE',
        ),
      ),
    );
    
    
    // OR
    
    $args = array(
      'post_type'      => 'cafe',
      'posts_per_page' => 12,
      'rewrite' => array( 'slug' => 'cafe', 'with_front' => true),
      'meta_query' => array(
        array(
          'key'     => 'cafe_state',
          'value'   => $cafe_state,
          'compare' => 'LIKE',
        ),
        array(
          'key'     => 'cafe_area',
          'value'   => $cafe_area,
          'compare' => 'LIKE',
        ),
        array(
          'key'     => 'cafe_name',
          'value'   => $cafe_name,
          'compare' => 'LIKE',
        ),
        array(
          'relation' => 'OR',
              array(
                'key'     => 'cafe_facilities',
                'value'   => '"test"',
                'compare' => 'LIKE',
              ),
              array(
                'key'     => 'cafe_facilities',
                'value'   => '"tests"',
                'compare' => 'LIKE',
              ),
         ),
      ),
    );
    

    You should be cautious when adding additional meta queries because the slow down site performance.

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

You must be logged in to reply to this topic.