This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Array

#27521
Ernest MarcinkoErnest Marcinko
Keymaster

Hi,

The plugin should recognize some basic array structures in the custom field values, and will print them comma separated. This may require a custom code to parse and print correclty.

I would try this custom code variation first. Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

add_filter( 'asp_results', 'asp_custom_field_to_results', 10, 1 ); 
function asp_custom_field_to_results( $results ) {
  $custom_fields = 'alternate_color_name'; 	        // Enter the custom field names, comma separated
  $field = 'content';               					// 'title' or 'content'
  $position = 'after';           					// 'before' or 'after'
  $delimiter = ' ';               					// character between the field value and the field
	
  $fields = explode(',', $custom_fields);
  foreach ( $fields as $custom_field ) {
	  $custom_field = trim($custom_field);
	  foreach ($results as $k=>&$r) {
		if ($r->content_type != 'pagepost') continue;
		if ( function_exists('get_field') )
			$meta_value = get_field( $r->id, $custom_field, true ); // ACF support
		else
			$meta_value = get_post_meta( $r->id, $custom_field, true );
		if ( is_array($meta_value) ) {
			$meta_value = implode(', ', $meta_value);
		}		
		// Modify the post title to add the meta value
		if ( $meta_value != '' ) {
		  if ( $field == 'title' ) {
			if ( $position == 'before' )
			  $r->title  = $meta_value . $delimiter . $r->title;
			else
			  $r->title  .= $delimiter . $meta_value;
		  } else {
			if ( $position == 'before' )
			  $r->content  = $meta_value . $delimiter . $r->content;
			else
			  $r->content  .= $delimiter . $meta_value;
		  }
		}
	  }
  }
 
  return $results;
}