【问题标题】:WP_Query with Advanced Custom Fields Meta Query Doesn't Work具有高级自定义字段元查询的 WP_Query 不起作用
【发布时间】:2016-08-24 20:10:03
【问题描述】:

我有一个构建 Bootstrap 轮播的 WP_Query。我添加了一个高级自定义字段单选按钮,该按钮将允许客户在帖子上选择是/否选项来“功能”这个。我的 WP_Query 在没有元查询的情况下完美运行,但是当我添加它时,它没有给出任何结果。我不确定这是否是因为它在 archive.php 上。我添加了一个动态类别,该类别仅显示当前类别中的精选帖子(也可以完美运行)。只是 ACF 似乎不起作用。我已经验证了键和值都存储在数据库中,就像这里调用的一样。我什至使用 get_field() 语句成功地回显了该值,以确保它有效。我难住了。任何建议将不胜感激。

这是我的代码:

<?php
    $qcatid = get_queried_object(); // So we can get the query category ID
    $args2=array(
      'post_type' => 'post',
      'post_status' => 'publish',         
      'cat' => $qcatid->cat_ID, // Query the proper category
      'orderby' => 'date',          
      'posts_per_page' => -1,
      'meta_query' => array(              
          array(
            'key' => 'feature_in_slider_carousel',
            'value' => 'Yes'
          )
        )

    );
    $mycat_query = null; 
    $mycat_query = new WP_Query($args2);
    if( $mycat_query->have_posts() ) { 
    $slide_count = 0;
?>
<section id="featured-posts">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <hr />
            </div>
        </div>
    </div>
    <div id="featured-carousel" class="carousel slide" data-ride="carousel">                  
      <div class="carousel-inner" role="listbox">
<?php
  while ($mycat_query->have_posts()) : $mycat_query->the_post(); ?>
    <div class="item <?php if ($slide_count == 1) { echo 'active';} ?>">
        <div class="row">
            <div class="col-sm-2 col-sm-offset-3">
                <div class="whitepaper-img">
                <a href="<?php the_permalink(); ?>">
                    <?php
                        include( TEMPLATEPATH . '/inc/icon-include.php' );
                        if (has_post_thumbnail( $post->ID ) ) {
                        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'whitepaper-carousel' ); ?>
                        <img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" class="img-responsive" />
                    <?php } 
                        else { ?>
                        <img src="<?php bloginfo('template_directory'); ?>/img/default-whitepaper.png" alt="<?php the_title(); ?>" class="img-responsive" />    
                    <?php } ?>
                    </a>
                </div>
            </div>
            <div class="col-sm-5">                  
                <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
                <?php the_excerpt();?>
                <div class="post-tags">
                    <?php
                        $posttags = get_the_tags();
                        if ($posttags) {
                            echo '<ul class="list-inline">';
                          foreach($posttags as $tag) {
                            echo '<li><a href="'. get_bloginfo('url') .'/tag/' . $tag->slug . '/">' . $tag->name . '</a></li>'; 
                          }
                          echo '</ul>';
                        }
                    ?>
                    </div>
            </div>
        </div>

    </div>
  <?php 
  $slide_count++;
  endwhile; ?>
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#featured-carousel" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#featured-carousel" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>

</div>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <hr />
            </div>
        </div>
    </div>
</section>
<?php  }
  wp_reset_query();
?>

【问题讨论】:

  • 您是否尝试过在 元查询 中使用 field_key 而不是 field_name 作为 key 值?

标签: wordpress advanced-custom-fields


【解决方案1】:

据我所知,您的代码没问题。我在 VVV 上运行并填充了 WP 虚拟内容的本地站点进行了测试。

为了测试,我完全按照您的描述创建了自定义字段,可能的例外是我将单选按钮值/标签对设置为:

yes : Yes
no : No

我将默认值设置为否。

我使用了一个名为“自定义字段批量编辑器”的插件来批量为现有帖子分配值,然后使用(大部分)您的代码设置一个 archive.php,一切都按预期工作。

$qcatid = get_queried_object();

$args2       = array(
  'post_type'      => 'post',
  'post_status'    => 'publish',
  'orderby'        => 'date',
  'cat'            => $qcatid->cat_ID,
  'posts_per_page' => -1,
  'meta_query'     => array(
    array(
      'key'   => 'feature_in_slider_carousel',
      'value' => 'Yes'
    )
  )
);

$mycat_query = null;
$mycat_query = new WP_Query($args2);

if ($mycat_query->have_posts()) :
  echo '<ul>';

  while ($mycat_query->have_posts()) : $mycat_query->the_post();

    echo '<li>' . get_the_title() . '</li>';

  endwhile;
  wp_reset_postdata();
  echo '</ul>';
endif; 

值得注意的是,这在没有 meta_query 的情况下也有效:

$args2 = array(
  'post_type'      => 'post',
  'post_status'    => 'publish',
  'orderby'        => 'date',
  'cat'            => $qcatid->cat_ID,
  'posts_per_page' => -1,
  'meta_key'       => 'feature_in_slider_carousel',
  'meta_value'     => 'Yes'
);

在这两种情况下访问类别存档页面只会返回标记为“是”且来自相应类别的帖子。

过去,我在对 ACF 标签/值进行了某种编辑后遇到了问题,例如将 foo : Bar 更改为 foo : Bat。过去发生这种情况时,我发现删除并重新创建具有不同名称的字段对我有用。

很抱歉,我无法提供更多帮助。

【讨论】:

    猜你喜欢
    • 2019-07-14
    • 2016-09-05
    • 2021-07-07
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    相关资源
    最近更新 更多