Support

Account

Home Forums General Issues Query ACF field with nested AND OR LIKE

Solved

Query ACF field with nested AND OR LIKE

  • Hello,
    I am desperately trying to make the following query work:

    
    $args = array (
      'posts_per_page'  => -1,
      'post_status' => 'publish',
      'post_type'         => array( 'my-cpt' ),
      'meta_query'  => array(
        'relation'     => 'AND',
        array( 
          'relation' => 'OR', 
          array( 
            'place_clause_1' => array(
                          'key'     => 'place',
                          'value'    =>  '"14979"',
                          'compare'   => 'LIKE',
                          ),
            'place_clause_2' => array(
                          'key'     => 'place',
                          'value'    =>  '"8917"', 
                          'compare'   => 'LIKE',
                          ),
            ),
        ),    
        'date_clause' => array(
          'key'     => 'date',
          'value'    => date('YmD'),
          'compare'   => '>=',
        ),
        'heure_clause' => array(
          'key'     => 'time_start',
          'type'    => 'NUMERIC',
          'compare'   => 'EXISTS',
        ), 
    
      ),
      'orderby' => array(
        'date_clause' => 'ASC', 'heure_clause' => 'ASC'
      )
    );
    $cal_query = new WP_Query( $args );
    

    It returns nothing, whereas the following query returns 1 result (which is what I would expect from the previous query as well…)

    
    $args = array (
      'posts_per_page'  => -1,
      'post_status' => 'publish',
      'post_type'         => array( 'my-cpt' ),
      'meta_query'  => array(
        'relation'     => 'AND',
        array( 
          'relation' => 'OR', 
          array( 
            'place_clause_1' => array(
                          'key'     => 'place',
                          'value'    =>  '"14979"',
                          'compare'   => 'LIKE',
                          ),
            //'place_clause_2' => array(
            //              'key'     => 'place',
            //              'value'    =>  '"8917"', 
            //              'compare'   => 'LIKE',
            //              ),
            ),
        ),    
        'date_clause' => array(
          'key'     => 'date',
          'value'    => date('YmD'),
          'compare'   => '>=',
        ),
        'heure_clause' => array(
          'key'     => 'time_start',
          'type'    => 'NUMERIC',
          'compare'   => 'EXISTS',
        ), 
    
      ),
      'orderby' => array(
        'date_clause' => 'ASC', 'heure_clause' => 'ASC'
      )
    );
    $cal_query = new WP_Query( $args );
    

    I use ACF Pro 6.2.7 with WP 6.4.3 if it matters.

  • I can mention as well that in my example above the only ‘place’ field satisfying both the ‘date_clause’ and the ‘heure_clause’ contains a:1:{i:0;s:5:"19479";}.

  • Eventually! Bad nesting syntax of mine, not related to ACF, sorry.

    
    $args = array (
      'posts_per_page'  => -1,
      'post_status' => 'publish',
      'post_type'         => array( 'my-cpt' ),
      'meta_query'  => array(
        'relation'     => 'AND',
        array( 
          'relation' => 'OR', 
           array(
                          'key'     => 'place',
                          'value'    =>  '"14979"',
                          'compare'   => 'LIKE',
                          ),
           array(
                          'key'     => 'place',
                          'value'    =>  '"8917"', 
                          'compare'   => 'LIKE',
                          ),
        ),    
        'date_clause' => array(
          'key'     => 'date',
          'value'    => date('YmD'),
          'compare'   => '>=',
        ),
        'heure_clause' => array(
          'key'     => 'time_start',
          'type'    => 'NUMERIC',
          'compare'   => 'EXISTS',
        ), 
    
      ),
      'orderby' => array(
        'date_clause' => 'ASC', 'heure_clause' => 'ASC'
      )
    );
    $cal_query = new WP_Query( $args );
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.