Home Forums Add-ons Repeater Field Get users which contain specific value in the repeater


Get users which contain specific value in the repeater

  • 1) How can I get all users*
    2) How can I count all users*

    *which contain specific value in the repeater?

    lets say I have

    – repeater_field_sample
    — field_row: 121
    — field_row: 17

    – repeater_field_sample
    — field_row: 13
    — field_row: 121

    – repeater_field_sample
    — field_row: 2

    In first case I would like to pull all users with field_row = 121 so the request should bring me 2 results, USER_1 and USER_2… in second case I just want to get total number of users according to the conditions so 2 users.

    What I was trying:

    $args = array(
    							'meta_query'	=> array(
    								'relation' => 'OR',
    												array('key' => 'repeater_field_sample_0_field_row','compare' => 'EXISTS','value'=>'121'),
    												array('key' => 'repeater_field_sample_1_field_row','compare' => 'EXISTS','value'=>'121'),

    I don’t really like this solution.

    If I use only one array for ex. “repeater_field_sample_0_field_row” then I get result from the row where this number is contained but when I use two like above then get nothing even I use ‘OR’ relation. Second thing is that I’m gonna use multiple keys and several different queries so I don’t really want to create 100 relations, would prefer to use ‘regex’ or ‘like’, whathever is better n will work. Any solution?

  • There is an example on this page for queries based on sub fields for posts and the same principle could be applied to a user query

    I personally don’t like that option and find that it’s one of the repeater fields shortcomings. My personal preference is to create an acf/save_post filter that takes the content for the repeater and puts it into standard WP meta fields in the database so that I can search for them without all the extra overhead. An extremely simple example of this with a repeater would be…

    add_action('acf/save_post', 'copy_repeater_to_standard')'
    function copy_repeater_to_standard($post_id) {
      if (substr($post_id, 0, 5) != 'user_') {
        // not a user, bail
      $repeater = 'my_repeater'; // name of repeater field
      $sub_field = 'my_sub_field'; // name of sub field
      // the name of the new field. This is not an acf field
      // just a custom user meta field
      // that can be used for searching
      $new_field = 'converted_sub_field';
      $user_id = intval(substr($post_id, 5));
      // first step is to delete any existing content from the new field
      delete_user_meta($user_id, $new_field);
      // now add content from the repeater
      // an array to hold the values we've added
      // because there is no point in saving them twice
      $stored = array();
      if (have_rows($repeater, $post_id)) {
        while (have_rows($repeater, $post_id)) {
          $value = get_sub_field($sub_field, $post_id);
          if ($value && !in_array($value, $stored)) {
            $stored[] = $value;
            // last parameter is false so that multiple values will be stored
            add_post_meta($user_id, $new_field, $value, false);

    in this case it is specific to a user, but could also be used for posts and terms by using the correct functions.

    This took me only a few minutes to write and it may store a bit more in the db, but I’ve save myself a lot of trouble. Also, the queries on this new field will be much faster than the like queries that are needed to search a repeater sub field so it improves site performance.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Get users which contain specific value in the repeater’ is closed to new replies.