Home Forums Add-ons Repeater Field Repeater subfields not working (with array) Reply To: Repeater subfields not working (with array)

  • The main problem here is that there is no way at all, using WP_Query to match up the values represented by the % pattern in your field names. So for example, it will match if the combination of awards_given_1_year, awards_given_2_program and awards_given_3_level meet the requirements.

    The number of queries you’d need to do with WP_Query would be uncountable.

    It might be possible to remove the % and add enough meta_query conditions to do the work, but this would in all likelihood, time out the page.

    This cannot even be done with a single query using $wpdb, because there is no single MySQL query that can be constructed to do the work. I have looked into this, and have asked the question in various places, including here, knowing that I would probably never get an answer to the question, but not wanting to give up on the idea.

    Anyway, I just wanted to give you a little background on the difficulty of doing this type of querying.

    This is how I solve the question. 1st thing is that I never use repeaters for anything that I may want to search on if I can help it at all. I will spend days looking at alternate solutions. I’m pretty sure that this is too late for your current project because I’m pretty sure that this question reminds me of previous questions here related to the same type of setup of fields.

    2nd, if I must use a repeater and cannot find another solution, or for some reason it’s too late to change it, then what I do is I create an acf/save_post filter that gets the values from the repeater and puts them into a form that is searchable by much simpler methods.

    // priority 20 is after ACF is done
    add_action('acf/save_post', 'convert_repeater_to_wp', 20);
    function convert_repeater_to_wp($post_id) {
      if (get_post_type($post_id) != 'profile') {
        // not the right post type
      // create a meta key to store the new values under
      $meta_key = 'combined_awards_given_repeater'
      // clear all of the existing values for the field before adding new values
      // to make sure it's update correctly
      delete_post_meta($post_id, $meta_key);
      // now we add the values of the repeater to this field
      if (have_rows('awards_given')) {
        while(have_rows('awards_given')) {
          $value = get_sub_field('year').' '.
                     get_sub_field('program').' '.
          add_post_meta($post_id, $meta_key, $value, false);
          // the last value "false" tells WP that this meta key can have multiple values
        } // end while have rows
      } // end if have rows
    } // end function

    with the above function in place you can now alter your query to

    $args = array(
      'posts_per_page'  => -1,
      'post_type'    => 'profiles',
      'meta_query'  => array(
          'key'    => 'combined_awards_given_repeater',
          'compare'  => '=',
          'value'    => '2017 Media Relations GRAND PRIZE',

    the only hurdle you have is getting the existing values into this field. You can either manually open and update every post if the number is limited… or you can build a cron to do the work by getting every post of the post type, looping through them all and doing the same thing that the function does for every post.