wordpressadvanced-custom-fieldsmeta-querypost-type

ACF Meta_Query and Get_posts is not working


I am trying to query custom post type but I keep getting no result. Is my meta_query the culprit? What is wrong with this code? I am trying to spot the issues but i cannot find anything.

$catname = 'travel';
$priority ='high';
$status = 'incomplete';
$args = array(     
          'post_type'       =>   'my_gallery_post',
          'orderby'         =>   'id',
            //'fields'      => 'ids',
          'sort_order'      =>   'asc',
            'post_status'    => 'publish',
          'posts_per_page'  =>    1,
           'meta_query' => array(
                    'relation' => 'AND',
                    array(
                        'key' => 'category_it_belongs',
                        'value'   => $catname,
                        'compare' => '='
                    ),
                    array(
                        'key' => 'levelof_priority',
                        'value'   => $priority,
                        'compare' => '='
                    ),
                    array(
                        'key' => 'progress',
                        'value'   => $status,
                        'compare' => '='
                    ),
                )
            
        
      );
$mypost = get_posts($args);

print_r($mypost);

Output

Array()

Solution

  • I solved the problem by using array for 'levelof_priority'. The field type for 'levelof_priority' is radio button whereas the other 2 are text field.

    Below are my final code that's working

    $catname = 'travel';
    $priority ='high';
    $status = 'incomplete';
    $args = array(     
              'post_type'       =>   'my_gallery_post',
              'orderby'         =>   'id',
                //'fields'      => 'ids',
              'sort_order'      =>   'asc',
                'post_status'    => 'publish',
              'posts_per_page'  =>    1,
               'meta_query' => array(
                        'relation' => 'AND',
                        array(
                            'key' => 'category_it_belongs',
                            'value'   => $catname,
                            'compare' => '='
                        ),
                        array(
                            'key' => 'levelof_priority',
                            'value'   => array($priority),
                            'compare' => '='
                        ),
                        array(
                            'key' => 'progress',
                            'value'   => $status,
                            'compare' => '='
                        ),
                    )
                
            
          );
    $mypost = get_posts($args);
    
    print_r($mypost);