2
votes

Been reading up on info for querying ACF Fields in wordpress here, having trouble with this bit though:

$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);

Basically, just trying to query all posts that have an ACF Repeater field, called patents that has at least 1 patent inside of it. How to do this?

4

4 Answers

5
votes

I just had to deal with this myself, and what ended up working for me was querying for all posts where the value of the repeater field was greater than 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 to search for 'patents' = 1 only returned posts where the repeater field had one value.

0
votes

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

Try to access the repeater field first, then call the WP_Query. If you were to do something like:

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

Then call the sub_field and/or in this case the WP_Query.

There is also this syntax, you can gather all the posts you need then scroll through posts:

if( have_rows('repeater_field') ):

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

    $sub = get_sub_field('sub_field');

Maybe you can call the wp_query on this level?

0
votes

just check if the value is 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
votes

Try this ..

This is not the standard solution for achieving the target but it will work

Get all the post IDs whose repeater field(s) are there

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

Then run another Loop to get only those posts by their 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

}}