2
votes

I would like to query an ACF repeater field. I've a repeater field called Library (in a CPT called Book) and, in this repeater, I have a relation field called Library (that is connected to an other custom post type called Library). It is this field that I would like to query*.

I look to recover, thanks to an unique value given by the user (selected thanks to a select), all the books that are in relation with this Library.

Ex : 'Library 1' is selected. Return : 'Harry Potter 1' and 'Harry Potter 2'.

Trials (not working)

function my_posts_where( $where ) {

$where = str_replace("meta_key = 'library_$", "meta_key LIKE 'library_%", $where);
return $where; } add_filter('posts_where', 'my_posts_where');
$library= $_GET['library'];

$v_args = array(
        'post_type'     =>  'book', 
        'meta_query'    =>  array(
                                array(
                                    'key'     => 'library_$_library',
                                    'compare' => '=', 
                                    'value'   => $library, 
                                ),
                            )
    ); $Query = new WP_Query($v_args);

    if($Query->have_posts()) :
        while($Query->have_posts()) : $Query->the_post();
            ...
        endwhile;

    else : 
        ...
    endif;

And this

    $library= $_GET['library'];

    $v_args = array(
        'post_type'     =>  'book', 
        'meta_query'    =>  array(
                                array(
                                    'key'     => 'library',
                                    'value'   => $library,
                                    'compare' => 'LIKE', 
                                ),
                            )
    );

$Query = new WP_Query($v_args); 

I searched on the Internet, I could not resolve my problem ...

Thanks a lot.

-* : I also have a repeater field called Tags and in it a Taxonomy field in relation with tags. I would like to show all the books that have thig tag.

1

1 Answers

2
votes

This is a bit late, but in case this helps anyone else, this works for me:

//Since the changed behaviour of esc_sql() in WordPress 4.8.3, 
//cannot use the % character as a placeholder, hence need to alter 'where' close:
function my_posts_where( $where ) {
    $where = str_replace("meta_key = 'library_$", "meta_key LIKE 'library_%", $where);
    return $where;
}
add_filter('posts_where', 'my_posts_where');

$library_id = $_GET['library']; //get querystr var

$args = array(
     'post_type'      => 'book',
     'posts_per_page' => -1,
     'meta_query'     => array(
          array(
               'key' => 'library_$_library', // our repeater field post object
               'value' => '"'. $library_id .'"', //matches exactly "123", not just 123 - prevents a match for "1234"
               'compare' => 'LIKE'
          )
     )
);
$query = new WP_Query($args);

if ($query->have_posts()): 
    while ($query->have_posts()) : $query->the_post();
        echo $post->post_title.'<br>';
    endwhile; 
endif;

wp_reset_query();