2016-03-03 63 views
2

一直在阅读了关于在WordPress的here查询ACF字段信息,遇到问题,这一点,虽然:ACF领域查询通过中继场不空

$args = array(
    'posts_per_page' => -1, 
    'post_type' => 'team_member', 
    'status' => 'publish', 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'meta_query' => array(
     array(
      'key' => 'patents', 
      'value' => array(''), 
      'compare' => 'NOT IN' 
     ) 
    ) 
); 

$the_query = new WP_Query($args); 

基本上,只是想查询有一个ACF的所有帖子中继器字段,称为patents,其中至少有1项专利。这个怎么做?

回答

0

http://www.advancedcustomfields.com/resources/the_repeater_field/

尝试首先访问中继场,然后调用WP_Query。如果你想做类似的事情:

if(get_field('repeater_field_name', $post_id)) 

然后调用子字段和/或在这种情况下WP_Query。

也有这种语法,你可以收集你需要再通过岗位滚动的所有帖子:

if(have_rows('repeater_field')): 

while(have_rows('repeater_field')) : the_row(); 

    $sub = get_sub_field('sub_field'); 

也许你可以调用这个级别的wp_query?

+0

这不能工作,因为我需要访问没有任何空中继器字段的所有帖子类型,我不能首先查询帖子的'$ post_id'。 –

+1

还有https://wordpress.org/support/topic/acf-repeater-field –

+0

谢谢,我现在将查看'tax_query'选项。 –

0

只是检查,如果该值true

$args = array(
    'posts_per_page' => -1, 
    'post_type' => 'team_member', 
    'status' => 'publish', 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'meta_query' => array(
     array(
      'key' => 'patents', 
      'value' => true, 
      'compare' => '=' 
     ) 
    ) 
); 

$the_query = new WP_Query($args); 
+0

当我这样做的时候,这给了我一个空的结果,而且我肯定在'patents'中继器字段里有一些带有repeater项目的帖子。 –

+0

这段代码会给你任何至少有1行acf中继器提交的文章(专利) – Mark

0

试试这个..

这不是实现目标的标准解决方案,但它会 工作

获取所有有中继器字段的帖子ID

<?php 
$args = array(
    'posts_per_page' => -1, 
    'post_type' => 'team_member', 
    'status' => 'publish', 
    'orderby' => 'title', 
    'order' => 'ASC', 
); 

$the_query = new WP_Query($args); 

// The Loop 
if ($the_query->have_posts()) { 
    while ($the_query->have_posts()) { 
     $the_query->the_post(); 
     if (have_rows('patents')){ 
      $allowed_ids[] = get_the_ID(); 
     } 
    } 
} 
/* Restore original Post Data */ 
wp_reset_postdata();?> 

然后运行另一个循环通过他们的IDS只得到这些职位

$args['post__in'] = $allowed_ids; 

    $the_query = new WP_Query($args); 
// The Loop 
    if ($the_query->have_posts()) { 
     while ($the_query->have_posts()) { 
      $the_query->the_post(); 
$patents = get_field('patents');//array 

}} 
+0

谢谢,但这并不能帮助我获得ACF中继器字段,其中至少有1项。这只是获得帖子的ID。 –

+0

在第二个循环中,您可以获得中继器值。请参阅编辑答案 –

4

我不得不处理这个自己,什么结束了,我的工作是在查询所有帖子,其中的价值中继器场是大于0

$args = array(
    'posts_per_page' => -1, 
    'post_type' => 'team_member', 
    'status' => 'publish', 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'meta_query' => array(
     array(
      'key' => 'patents', 
      'value' => 0 
      'compare' => '>' 
     ) 
    ) 
); 

Mark's answer搜索的专利'= 1只返回讯息,其中所述中继器字段具有一个值。

+0

谢谢,这看起来很有希望。将不得不记住这一点。 :) –