Support

Account

Home Forums Add-ons Repeater Field Repeater field returns string

Solving

Repeater field returns string

  • Hi there, I am trying to access a repeater field using the following code:

    This is outside the loop on a taxonomy term page. There is a post object field within the taxonomy term. I am trying to take the post object selected within the taxonomy term and show images that are attached to the selected post.

    			$queried_object = get_queried_object(); 
    			$taxonomy = $queried_object->taxonomy;
    			$term_id = $queried_object->term_id; 
    			
    			$post_object = get_field('featured_project', $taxonomy.'_'.$term_id);
    			
    			if( $post_object ):
    				if( have_rows('project_images', $post_object) ):
    					while ( have_rows('project_images', $post_object) ) : the_row();
    						the_sub_field('project_image');
    					endwhile;
    				endif;
    			endif;

    When I print out the repeater field using get_field(‘project_images’, $post_object’) I receive a string with the total number of rows within the repeater.

    The repeater field seems to work fine on the single post page.

  • This is something that has been reported a lot in the past and it almost always ends up being something that is interfering with ACF or some other problem. Your code appears correct and should be working.

    The first steps you’ll need to take are is to disable any other plugins and try it in a different theme. If we can narrow down the cause it will make it easier to find.

  • Thanks for the response John. Are there any common culprits that I can look for? I am using a relatively barebones Genesis child theme.

  • Here’s another recent topic, http://support.advancedcustomfields.com/forums/topic/acf-pro-options-pages-with-repeaters-not-working/, but I do remember others that use genesis having this problem.

    But this is usually a problem when people try to use get_field to return then entire array and not something that is usually a problem when using the acf have_rows and while loop, so this is a little confusing and I’m not sure that it’s the same issue or that my first post is right

    It should work no matter what is in $post_object (object or ID) but you could try

    
    if( $post_object ):
      if( have_rows('project_images', $post_object->ID) ):
        while ( have_rows('project_images', $post_object->ID) ) : the_row();
    	the_sub_field('project_image');
        endwhile;
      endif;
    endif;
    

    The only other thing I see to ask about is what it the ‘project_image’ sub field set to return, an image array, ID or URL?

  • Right now $post_object is returning the ‘featured_project’ post ID when I use get_field. I am using it elsewhere without an issue, so the ID it is grabbing is correct.

    The ‘project_image’ field is set to return an image array, however, I also tried just the URL.

    I am using the same exact code (minus the added ID option) on a single post page and it works just fine.

  • If you have the image field to return an image array then the_sub_field('project_image'); may not output anything. Try print_r(get_sub_field('project_image'));

    And you’re correct, you don’t need the ->ID part if you’re returning the ID for the post object field.

  • print_r(get_sub_field('project_image')); won’t get called within my current loop because if( have_rows('project_images', $post_object) ): is false (because it is returning a string). Is there a different way to get at the sub field of a repeater?

  • what is the output if you do this

    
    $post_object = get_field('featured_project', $taxonomy.'_'.$term_id);
    if ($post_object) {
      print_r(get_field('project_images', $post_object));
    }
    
  • Sorry for the delay on this. I really do appreciate your responses. Your code returns the string (5)…which is the number of image fields there are in the repeater.

  • The code I posted should be returning an array containing the values in the repeater and sub fields rather than the number of rows in the repeater. There is something on the site you’re working on that’s interfering with what ACF is returning, I wouldn’t be surprised if it has something to do with the theme.

    But I don’t see any reason why the have_rows() loop should not be working if you’re getting a value. A string value of ‘5’ will evaluate to true, not false. Your original code should be working. Again, if it’s not then there is something that’s causing it not to work.

    I don’t have access to Genesis and I don’t use it. I would start by trying one of the default 20XX WP themes to see if I working. If that didn’t help then I’d remove any other plugins.

  • Alright, so I have run into this thread numerous times and it never helped solve the issue.

    I usually am writing my own plugin and using get_field seems to stop working with the identical behavior that @corydavidwilliam described. It’s not because of the theme or other plugins. It’s because of my own code. So I’ve isolated the problem and wanted to share, in case others run into the same.

    The following code was the culprit, the cause of my repeater returning string “1” instead of array( ‘date’ => ‘9/20/2017’, ‘time’ => ’12:30pm’ ).

    /**
     * Sort open house in RSS FEEDS by open house date, and hide expired entries.
     *
     * @param $query
     */
    function bn_open_house_sort_rss_by_open_house_date( $query ) {
    	if ( !is_feed() ) return;
    	if ( get_query_var('post_type') != 'open_house' ) return;
    	
    	if ( !($query instanceof WP_Query) ) return;
    	
    	$query->set('order', 'ASC');
    	$query->set('orderby', 'meta_value_num');
    	$query->set('meta_key', 'open_house_date_timestamp');
    
    	$query->set('meta_query', array(
    		array(
    			'key' => 'open_house_date_timestamp',
    			'value' => strtotime('-6 Hours'), // Do not show posts from several hours earlier
    			'compare' => '>=',
    			'type' => 'NUMERIC',
    		)
    	));
    }
    add_action( 'pre_get_posts', 'bn_open_house_sort_rss_by_open_house_date' );

    The problem with the above code is that get_query_var(‘post_type’) returns “open_house” during the loop of an RSS feed, as expected. However, get_field() performs it’s own query which tries to get the post type “acf-field” (the field group or specific field, I assume).

    Because my code is adding a meta_query, the acf field query never returns a result. My meta query should only apply to open houses – not ACF fields!

    The solution is to instead check the post type of the current query, and NOT use get_query_var.

    Replace: if ( get_query_var(‘post_type’) != ‘open_house’ ) return;

    With: if ( $query->get(‘post_type’) != ‘open_house’ ) return;

    This makes it so I still have the same behavior as before, except it doesn’t interfere with the get_field() function.

    Note that this won’t be an exact solution for everyone, but since this pops up so often I’m guessing people need to check into the pre_get_posts hook and see if that’s where the problem is coming from. I didn’t expect get_field() to perform a WP_Query.

  • I was having exactly the same problem. Things were working well until I introduced some filters to include in the search results matches by taxonomy terms. The filters I was using came from a 3rd party site. The code was the one below. On its own it works well and was helping with my search results (so if someone enters a tag name it will also return associated posts). But there’s something that caused conflicts with ACFPro.

    function atom_search_where($where){
      global $wpdb;
      if (is_search())
        $where .= "OR (t.name LIKE '%".get_search_query()."%' AND {$wpdb->posts}.post_status = 'publish')";
      return $where;
    }
    
    function atom_search_join($join){
      global $wpdb;
      if (is_search())
        $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id";
      return $join;
    }
    
    function atom_search_groupby($groupby){
      global $wpdb;
    
      // we need to group on post ID
      $groupby_id = "{$wpdb->posts}.ID";
      if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby;
    
      // groupby was empty, use ours
      if(!strlen(trim($groupby))) return $groupby_id;
    
      // wasn't empty, append ours
      return $groupby.", ".$groupby_id;
    }
    
    add_filter('posts_where','atom_search_where');
    add_filter('posts_join', 'atom_search_join');
    add_filter('posts_groupby', 'atom_search_groupby');
  • The reason this is causing an issue with ACF is that it is also altering the queries that ACF performs to do what it does.

    You will either need to adjust this somehow to only do this on the specific queries that you are interested in changing or you’ll need to use an alternate method of getting the values from the repeater.

    Beyond that I’m not going to be much help other than some advice.

    It may be possible to only add your filters for specific queries this way, but it’s only a guess and I don’t know if it will work or not.

    
    // using this function name for consitency
    add_filter('pre_get_posts', 'atom_pre_get_posts');
    function atom_pre_get_posts($query) {
      if (is_search()) {
        add_filter('posts_where','atom_search_where');
        add_filter('posts_join', 'atom_search_join');
        add_filter('posts_groupby', 'atom_search_groupby');
      }
    }
    

    The other choice is using get_post_meta instead of ACF function

    
    // get the number of rows
    $count = get_post_meta($post_id, 'repeater_field_name', true);
    
    // loop through repeater
    for ($i=0; $i<$count; $i++) {
      // get a sub field
      $value = get_post_meta($post_id, $repeater_field_name_'.$i.'_sub_field_name', true);
    }
    
Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘Repeater field returns string’ is closed to new replies.