【问题标题】:ACF Meta_Query and Get_posts is not workingACF Meta_Query 和 Get_posts 不起作用
【发布时间】:2022-01-06 15:53:39
【问题描述】:

我正在尝试查询自定义帖子类型,但一直没有结果。我的 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()

【问题讨论】:

  • 'sort_order' => 'asc' 替换为'order' => 'asc'
  • @ParthaviPatel 不。也没有用

标签: wordpress advanced-custom-fields meta-query post-type


【解决方案1】:

我通过使用'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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 2015-12-23
    • 2013-09-18
    • 1970-01-01
    • 2016-04-25
    相关资源
    最近更新 更多