How to use ACF repeater field with meta_query?

Hello there! Welcome back to another TechSolutionsHere article. In this article, I will show you how to use ACF repeater field with meta_query.

Let’s get started!

  • Trying to loop through an ACF repeater field using a meta_query, inside the WP_Query? All you need is a function to replace the way the meta_key works.

You need to add the following code into the theme/plugin functions.php/plugin-name.php file:

function tsh_replace_repeater_field( $where ) {
     $where = str_replace( "meta_key = 'yourrepeaterkey_$", "meta_key LIKE 'yourrepeaterkey_%", $where );
     return $where;
}
add_filter( 'posts_where', 'tsh_replace_repeater_field' );

Then go to your WP_Query’s arguments and use the meta_query parameter following:

$args = array(
     'post_type' => 'posttype', // your post type name here
     'meta_query' => array(
          array(
               'key' => 'yourrepeaterkey_$_repeateritemkey',
               'value' => 1,
               'compare' => '='
          )
     )
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
     // Do loop content
endwhile;
wp_reset_postdata();

That’s it

If you have any queries then please let me know in the comments section. If you think this post has saved your time, please subscribe to our newsletter for regular updates.

Happy coding.

Thank you in advance!

Leave a Reply

Your email address will not be published.