WordPress 高级自定义字段 - 元查询无结果


WordPress Advanced Custom Fields - Meta Query no result

我在我的WP中使用WPML和ACF。

现在我想列出类别 ID 399 中的帖子,其中包含 ACF 字段"organization_type"和值键"社交业务",但它们没有显示。

这是我的查询尝试:

        $args = array(
            'post_type'     => 'post',
            'cat'      => 399,
            'posts_per_page'    => -1,
            'meta_query'        => array(
                //'relation' => 'OR',
                array(
                    'key' => 'organization_type',
                    'value' => 'socialbusiness',
                    //'compare' => '='
                )
            )
        );
        //unset($args);
        $args = array(
            'numberposts' => -1,
            'post_type' => 'post',
            'cat' => 399,
            'meta_key' => 'organization_type',
            'meta_value' => 'socialbusiness'
        );
        // query
        query_posts( $args );
        while( have_posts() ) {

我做错了什么?

你应该

只有一个变量$args因为变量的第一个声明被第二个变量覆盖。

在您的情况下,您的代码应如下所示:

 <?php
 $args = array(
   'post_type' => 'post',
   'posts_per_page' => '-1',
   'tax_query' => array(
      array(
         'taxonomy' => 'category',
         'field' => 'id',
         'terms' => 399
         )
      ),
   'meta_query' => array(
      array(
       'key' => 'organization_type',
       'value' => 'socialbusiness',
       'compare' => '=',
       'type' => 'CHAR'
       ),
      )
   );
 $items = new WP_Query($args);
 ?>
 <?php if($items->have_posts()) : ?>
   <div class='item'>
      <?php while($items->have_posts()) : $items->the_post() ?>
         .....
      <?php endwhile ?>
   </div>
<?php endif ?>