Support

Account

Home Forums Bug Reports After update string conversion error in ACF Pro

Solved

After update string conversion error in ACF Pro

  • After updating to version 5.3.9 I get a fatal error when trying to output a repeater field.

    Catchable fatal error: Object of class WP_Term could not be converted to string in /Applications/MAMP/htdocs/wtb/wp-content/plugins/advanced-custom-fields-pro/api/api-template.php on line 372

    The error is triggered when I try to output a repeater field from an acf-extended category description.

    <?php 
    	// Get vars for taxonomy custom Fields
    	$queried_object = get_queried_object(); 
    	// $taxonomy = $queried_object->taxonomy;
    	// $term_id = $queried_object->term_id; 
    ?>
    <div class="col-md-6 mb-">
    <!-- desctription -->
    <?php 
    	echo get_field('beschreibung', $queried_object);
    ?>
    </div>
    <div class="col-md-6 mb-">
    <!-- get the accordion -->
    <?php if( have_rows('accordion', $queried_object)): ?>
    
    	<ul class="accordion">
    	<?php while( have_rows('accordion', $queried_object) ): the_row(); ?>
    
    		<li>
    			<div class="item parent js-accordion-trigger"><span><?php the_sub_field('titel'); ?></span><span class="fa fa-plus arrow"></span></div>
    			<div class="child">
    				<?php the_sub_field('ausklapptext'); ?>
    			</div>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
    </div>

    However simply outputting the wysiwyg-field does work:
    <?php echo get_field('beschreibung', $queried_object); ?>

  • You need to use the correct term id to get fields from a term and not the term object. I don’t know why one works and the other does not, but according to the ACF documentation using the term object is not something that supported

    
    <?php 
    	// Get vars for taxonomy custom Fields
    	$queried_object = get_queried_object(); 
    	$taxonomy = $queried_object->taxonomy;
    	$term_id = $queried_object->term_id; 
            $post_id = $taxonomy.'_'.$term_id;
    ?>
    <div class="col-md-6 mb-">
    <!-- desctription -->
    <?php 
    	echo get_field('beschreibung', $post_id);
    ?>
    
  • Thanks John, this fixed my issue.

    However, for the record, it is mentioned here in the documentation Get values from a taxonomy term (scroll to “Finding the active taxonomy & term on an archive page”) that you can use the object on archive page.

  • That’s actually interesting, I have not see that before, or maybe I did and don’t remember it. I’ll look into it.

  • The error is caused by the have_rows() function.

    The get_field() function sorts out the post ID by calling acf_get_valid_post_id() before using it, but in ‘have_rows()` the post_id is used on line 372 before it is converted on line 380. I will report this bug to the developer.

  • Hi @jann

    Thanks for the bug report.
    I’ve found and fixed the issue!

    Would you mind testing out the patch? Please edit the api/api-template.php file and replace the have_rows function with this:

    
    /*
    *  have_rows
    *
    *  This function will instantiate a global variable containing the rows of a repeater or flexible content field,
    *  afterwhich, it will determine if another row exists to loop through
    *
    *  @type	function
    *  @date	2/09/13
    *  @since	4.3.0
    *
    *  @param	$field_name (string) the field name
    *  @param	$post_id (mixed) the post_id of which the value is saved against
    *  @return	(boolean)
    */
    
    function have_rows( $selector, $post_id = false ) {
    	
    	// reference
    	$_post_id = $post_id;
    	
    	
    	// filter post_id
    	$post_id = acf_get_valid_post_id( $post_id );
    	
    	
    	// vars
    	$key = "selector={$selector}/post_id={$post_id}";
    	$active_loop = acf_get_loop('active');
    	$previous_loop = acf_get_loop('previous');
    	$new_parent_loop = false;
    	$new_child_loop = false;
    	$sub_field = false;
    	$sub_exists = false;
    	$change = false;
    	
    	
    	// no active loops
    	if( !$active_loop ) {
    		
    		// create a new loop
    		$new_parent_loop = true;
    	
    	// loop has changed
    	} elseif( $active_loop['key'] != $key ) {
    		
    		// detect change
    		if( $post_id != $active_loop['post_id'] ) {
    			
    			$change = 'post_id';
    				
    		} elseif( $selector != $active_loop['selector'] ) {
    			
    			$change = 'selector';
    				
    		} else {
    			
    			// key has changed due to a technicallity, however, the post_id and selector are the same
    			
    		}
    		
    		
    		// attempt to find sub field
    		$sub_field = acf_get_sub_field($selector, $active_loop['field']);
    			
    		if( $sub_field ) {
    			
    			$sub_exists = isset( $active_loop['value'][ $active_loop['i'] ][ $sub_field['key'] ] );
    			
    		}
    		
    		
    		// If post_id has changed, this is most likely an archive loop
    		if( $change == 'post_id' ) {
    			
    			if( empty($_post_id) && $sub_exists ) {
    				
    				// case: Change in $post_id was due to this being a nested loop and not specifying the $post_id
    				// action: move down one level into a new loop
    				$new_child_loop = true;
    			
    			} elseif( $previous_loop && $previous_loop['post_id'] == $post_id ) {
    				
    				// case: Change in $post_id was due to a nested loop ending
    				// action: move up one level through the loops
    				acf_remove_loop('active');
    			
    			} else {
    				
    				// case: Chang in $post_id is the most obvious, used in an WP_Query loop with multiple $post objects
    				// action: leave this current loop alone and create a new parent loop
    				$new_parent_loop = true;
    				
    			}
    			
    		} elseif( $change == 'selector' ) {
    			
    			if( $previous_loop && $previous_loop['selector'] == $selector && $previous_loop['post_id'] == $post_id ) {
    				
    				// case: Change in $field_name was due to a nested loop ending
    				// action: move up one level through the loops
    				acf_remove_loop('active');
    				
    			} elseif( $sub_exists ) {
    				
    				// case: Change in $field_name was due to this being a nested loop
    				// action: move down one level into a new loop
    				$new_child_loop = true;
    				
    			} else {
    				
    				// case: Chang in $field_name is the most obvious, this is a new loop for a different field within the $post
    				// action: leave this current loop alone and create a new parent loop
    				$new_parent_loop = true;
    				
    			}
    			
    		}
    	
    	// loop is the same	
    	} else {
    		
    		// do nothing
    		
    	}
    	
    	
    	// add parent loop
    	if( $new_parent_loop ) {
    		
    		// vars
    		$field = get_field_object( $selector, $post_id, false );
    		$value = acf_extract_var( $field, 'value' );
    		
    		
    		// add loop
    		acf_add_loop(array(
    			'selector'	=> $selector,
    			'name'		=> $field['name'], // used by update_sub_field
    			'value'		=> $value,
    			'field'		=> $field,
    			'i'			=> -1,
    			'post_id'	=> $post_id,
    			'key'		=> $key
    		));
    	
    	// add child loop
    	} elseif( $new_child_loop ) {
    		
    		// vars
    		$value = $active_loop['value'][ $active_loop['i'] ][ $sub_field['key'] ];
    		$post_id = $active_loop['post_id'];
    		
    		
    		// add loop
    		acf_add_loop(array(
    			'selector'	=> $selector,
    			'name'		=> $active_loop['name'] . '_' . $active_loop['i'] . '_' . $sub_field['name'], // used by update_sub_field
    			'value'		=> $value,
    			'field'		=> $sub_field,
    			'i'			=> -1,
    			'post_id'	=> $post_id,
    			'key'		=> $key
    		));
    		
    	}	
    	
    	
    	// update vars
    	$active_loop = acf_get_loop('active');
    	
    	
    	// return true if next row exists
    	if( $active_loop && is_array($active_loop['value']) && isset($active_loop['value'][ $active_loop['i']+1 ]) ) {
    		
    		return true;
    		
    	}
    	
    	
    	// no next row!
    	acf_remove_loop('active');
    	
    	
    	// return
    	return false;
      
    }
    
  • Thanks Elliot and John, for being so quick addressing this!

    The updated version 5.3.9.1 works both fine with string and object in my case.

  • Hi @jann

    Thanks for the reply. Happy to help and glad to have fixed the issue!

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

The topic ‘After update string conversion error in ACF Pro’ is closed to new replies.