2016-04-29 153 views
0

我有一个WP_Query构建一个Bootstrap轮播。我添加了一个高级自定义字段单选按钮,允许客户在帖子上选择“是/否”选项来“发送”此选项。我的WP_Query完美工作,没有Meta Query,但是当我添加它时,它没有任何结果。我不确定这是因为这是否在archive.php上。我添加了一个动态类别,它只显示当前类别的特色帖子(这也是完美的)。这只是ACF,似乎没有工作。我已经验证了这两个键值&正在被存储在数据库中,就像在这里被调用一样。我甚至用get_field()语句成功地回显了这个值,以确保它正常工作。我很难过。任何建议将不胜感激。WP_Query与高级自定义字段元查询不起作用

这里是我的代码:

<?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(); 
?> 
+0

你在_Meta Query_中试过用'field_key'而不是'field_name'作为_key_值吗? –

回答

0

至于我能告诉你的代码是好的。我测试了一个运行在VVV上的本地站点,并填充了WP虚拟内容。

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

yes : Yes 
no : No 

我设置的默认值,因为没有。

我使用了一个名为“Custom Field Bulk Editor”的插件来为现有的帖子批量赋值,然后使用(主要是)您的代码设置一个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' 
); 

访问类别存档页面只返回的帖子标记为Yes,并从相应类别。

我已经对ACF标签/值在过去做过某种编辑之后遇到了问题,例如将foo:Bar更改为foo:Bat。当过去发生这种情况时,我发现使用不同的名称删除和重新创建该字段已经为我工作。

对不起,我没有更多的帮助。