-2

我正在尝试查询自定义帖子类型,但一直没有结果。我的 meta_query 是罪魁祸首吗?这段代码有什么问题?我试图找出问题,但我找不到任何东西。

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

输出

Array()
4

1 回答 1

0

我通过使用'levelof_priority'的数组解决了这个问题。'levelof_priority' 的字段类型是单选按钮,而其他 2 个是文本字段。

下面是我工作的最终代码

$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);
于 2022-01-06T23:05:49.497 回答