【问题标题】:WordPress Advanced Custom Fields - Meta Query no resultWordPress 高级自定义字段 - 元查询无结果
【发布时间】:2015-02-16 06:32:57
【问题描述】:

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

现在我想列出来自类别 ID 399 的帖子,其中包含 ACF 字段“organization_type”和值键“socialbusiness”,但它们没有显示出来。

这是我的查询尝试:

        $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() ) {

我做错了什么?

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    你应该只有一个变量 $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 ?>
    

    【讨论】:

    • 谢谢,如果可行,我会很高兴,但它也没有显示任何结果!我检查了好几次类别 ID……太令人沮丧了。会是什么?
    猜你喜欢
    • 2016-04-07
    • 2018-02-04
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 2017-08-28
    • 2015-05-27
    • 2012-08-10
    • 2015-01-21
    相关资源
    最近更新 更多