Support

Account

Home Forums Add-ons Flexible Content Field get_post_meta for a repeater field in a flexible content field?

Solved

get_post_meta for a repeater field in a flexible content field?

  • Hi,

    I am using a custom walker to display certain custom fields in my navigation menu. I was using a simple repeater field for this at first, using the answer posted here: https://support.advancedcustomfields.com/forums/topic/get_post_meta-for-a-repeater-field/

    This worked great, however I decided to move the repeater field to a flexible content field. I got it working (kind of) by simply appending flexible_content_name_0_ in front of the code, however this only works when the repeater field is placed in the flexible content as the first item.

    I cannot guarantee the flexible content field is always first. How could I best achieve this?

  • Hi @teun

    The flexible content field and the repeater saves its data using a field name containing a series of ancestor names and the row id. The 0 on appended string represents the row number and thus you will need some logic to assess and assign the row number depending on the position of the nested repeater field.

    I hope this info helps.

  • Hi James,

    Thank you. I figured out it saved its data like this. The thing I’m struggling with is automatically finding the correct number. This is the code I currently have:

    $repeater_value = get_post_meta($item->object_id, 'flexibele_content_1_menukaart-repeater', true);
    		
    		if (1 == $depth and $repeater_value){
      			for ($i=0; $i<$repeater_value; $i++) {
        			$meta_key = 'flexibele_content_1_menukaart-repeater_'.$i.'_categorie';
        			$sub_field_value = get_post_meta($item->object_id, $meta_key, true);
        			$sub_field_link = (str_replace(' ', '-', strtolower($sub_field_value)));
        	$item_output .= '<a class="sublink" href="' . esc_attr( $item->url ) .'#'. $sub_field_link .'">';
        	$item_output .= $sub_field_value;
        	$item_output .= '</a>';
     			}
    		}
  • Hi @teun

    I’m afraid I don’t understand your purpose of doing this. Could you please let me know how the data looks like right now and what you were trying to achieve?

    Also, could you please let me know the result of your current code and how you want it to be?

    Thanks 🙂

  • Hi James,

    I have a flexible content field called “menu” (it’s a Restaurant menu), which has a repeater field, where each repeater field has a title. I want each title of this section as a dropdown menu for the corresponding page in the menu.

    See below my export of the particular field:

    array (
    					'key' => '586a0fb66fdef',
    					'name' => 'menukaart-layout',
    					'label' => 'Menukaart layout',
    					'display' => 'block',
    					'sub_fields' => array (
    						array (
    							'default_value' => 0,
    							'message' => '',
    							'ui' => 1,
    							'ui_on_text' => '',
    							'ui_off_text' => '',
    							'key' => 'field_586f538fcd098',
    							'label' => 'Toon menukaart menu',
    							'name' => 'toon_menukaart_menu',
    							'type' => 'true_false',
    							'instructions' => '',
    							'required' => 0,
    							'conditional_logic' => 0,
    							'wrapper' => array (
    								'width' => '',
    								'class' => '',
    								'id' => '',
    							),
    						),
    						array (
    							'sub_fields' => array (
    								array (
    									'default_value' => '',
    									'maxlength' => '',
    									'placeholder' => '',
    									'prepend' => '',
    									'append' => '',
    									'key' => 'field_586a0fe56fdf1',
    									'label' => 'Categorie',
    									'name' => 'categorie',
    									'type' => 'text',
    									'instructions' => '',
    									'required' => 0,
    									'conditional_logic' => 0,
    									'wrapper' => array (
    										'width' => '',
    										'class' => '',
    										'id' => '',
    									),
    								),
    								array (
    									'return_format' => 'array',
    									'preview_size' => 'thumbnail',
    									'library' => 'all',
    									'min_width' => '',
    									'min_height' => '',
    									'min_size' => '',
    									'max_width' => '',
    									'max_height' => '',
    									'max_size' => '',
    									'mime_types' => '',
    									'key' => 'field_586a0fea6fdf2',
    									'label' => 'Afbeelding',
    									'name' => 'afbeelding',
    									'type' => 'image',
    									'instructions' => '',
    									'required' => 0,
    									'conditional_logic' => 0,
    									'wrapper' => array (
    										'width' => '',
    										'class' => '',
    										'id' => '',
    									),
    								),
    								array (
    									'default_value' => '',
    									'new_lines' => '',
    									'maxlength' => '',
    									'placeholder' => '',
    									'rows' => '',
    									'key' => 'field_586a0ff56fdf3',
    									'label' => 'Omschrijving',
    									'name' => 'omschrijving',
    									'type' => 'textarea',
    									'instructions' => '',
    									'required' => 0,
    									'conditional_logic' => 0,
    									'wrapper' => array (
    										'width' => '',
    										'class' => '',
    										'id' => '',
    									),
    								),
    								array (
    									'sub_fields' => array (
    										array (
    											'default_value' => '',
    											'maxlength' => '',
    											'placeholder' => '',
    											'prepend' => '',
    											'append' => '',
    											'key' => 'field_586a10056fdf5',
    											'label' => 'Naam',
    											'name' => 'naam',
    											'type' => 'text',
    											'instructions' => '',
    											'required' => 0,
    											'conditional_logic' => 0,
    											'wrapper' => array (
    												'width' => '',
    												'class' => '',
    												'id' => '',
    											),
    										),
    										array (
    											'default_value' => '',
    											'maxlength' => '',
    											'placeholder' => '',
    											'prepend' => '',
    											'append' => '',
    											'key' => 'field_586a10106fdf6',
    											'label' => 'Prijs',
    											'name' => 'prijs',
    											'type' => 'text',
    											'instructions' => '',
    											'required' => 0,
    											'conditional_logic' => 0,
    											'wrapper' => array (
    												'width' => '',
    												'class' => '',
    												'id' => '',
    											),
    										),
    										array (
    											'default_value' => '',
    											'maxlength' => '',
    											'placeholder' => '',
    											'prepend' => '',
    											'append' => '',
    											'key' => 'field_586a101b6fdf7',
    											'label' => 'Omschrijving',
    											'name' => 'omschrijving',
    											'type' => 'text',
    											'instructions' => '',
    											'required' => 0,
    											'conditional_logic' => 0,
    											'wrapper' => array (
    												'width' => '',
    												'class' => '',
    												'id' => '',
    											),
    										),
    									),
    									'min' => 0,
    									'max' => 0,
    									'layout' => 'row',
    									'button_label' => 'Nieuw menu-item',
    									'collapsed' => '',
    									'key' => 'field_586a0ffc6fdf4',
    									'label' => 'Menu-item',
    									'name' => 'menu-item',
    									'type' => 'repeater',
    									'instructions' => '',
    									'required' => 0,
    									'conditional_logic' => 0,
    									'wrapper' => array (
    										'width' => '',
    										'class' => '',
    										'id' => '',
    									),
    								),
    							),
    							'min' => 0,
    							'max' => 0,
    							'layout' => 'row',
    							'button_label' => 'Nieuwe categorie',
    							'collapsed' => '',
    							'key' => 'field_586a0fbe6fdf0',
    							'label' => 'Menukaart',
    							'name' => 'menukaart-repeater',
    							'type' => 'repeater',
    							'instructions' => '',
    							'required' => 0,
    							'conditional_logic' => 0,
    							'wrapper' => array (
    								'width' => '',
    								'class' => '',
    								'id' => '',
    							),
    						),
    						array (
    							'clone' => array (
    								0 => 'group_5874c031b6839',
    							),
    							'prefix_label' => 0,
    							'prefix_name' => 0,
    							'display' => 'seamless',
    							'layout' => 'block',
    							'key' => 'field_5874c0d52c82f',
    							'label' => 'Informatie',
    							'name' => 'informatie',
    							'type' => 'clone',
    							'instructions' => '',
    							'required' => 0,
    							'conditional_logic' => 0,
    							'wrapper' => array (
    								'width' => '',
    								'class' => '',
    								'id' => '',
    							),
    						),
    					),
    					'min' => '',
    					'max' => '',
    				)

    I’d want the “Categorie” sub_field in my menu. I’m using a custom walker, the entire code can be seen below. It works, as long as the repeater field is the 2nd field in my flexible content.

    class Cusom_Menu extends Walker_Nav_Menu {
    	static $count=0;
    	function start_el(&$output, $item, $depth, $args) {
    		global $wp_query;
    		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    		
    		$class_names = $value = '';
    		$classes = empty( $item->classes ) ? array() : (array) $item->classes;
    		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
    		$class_names = ' class="page-item-'. $item->object_id .' ' . esc_attr( $class_names ) . '"';
    
    		if ($depth==0) self::$count=0;  // reset var when we are in first level
            if ($depth==1 && self::$count==1) {  // if we are in submenu and items count is 1...
    	       $output .= '<li class="terug mobile-only"><a href="#"><i class="fa fa-lg fa-chevron-left" aria-hidden="true"></i> Terug</a></li>';
    	       $parent = $item->menu_item_parent;
    	       $output .= '<li class="mobile-only"><a class="hoofd-item" href="#">hoi' . $parent->ID .'</a></li></a></li>';
            }
    
    		$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
    		$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
    		$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
    		$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
    		$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
    		$parent = wp_get_post_parent_id($item->object_id);
    		$thumbnail = '';
      		if ( has_post_thumbnail( $item->object_id ) and 1 == $depth ) {
       			$thumbnail = get_the_post_thumbnail( $item->object_id );
     		}
    		$item_output = $args->before;
    		
    		
    		if (1 == $depth and $thumbnail and $parent == 43){
    			$item_output .= '<a'. $attributes .' class="hoofdlink">';
    			$item_output .= '<div class="afbeelding">' . $thumbnail . '</div><span>';
    		} else {
    			$item_output .= '<a'. $attributes .' class="link-'. $depth .'">';
    		}
    		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    		if (1 == $depth and $thumbnail and $parent == 43){
    			$item_output .= '</span>';
    		}
    		$item_output .= '</a>';
    		$repeater_value = get_post_meta($item->object_id, 'flexibele_content_1_menukaart-repeater', true);
    		
    		if (1 == $depth and $repeater_value and $parent == 43){
      			for ($i=0; $i<$repeater_value; $i++) {
        			$meta_key = 'flexibele_content_1_menukaart-repeater_'.$i.'_categorie';
        			$meta_key = 'flexibele_content_1_menukaart-repeater_'.$i.'_categorie';
        			$sub_field_value = get_post_meta($item->object_id, $meta_key, true);
        			$sub_field_link = (str_replace(' ', '-', strtolower($sub_field_value)));
    		    	$item_output .= '<div><a class="sublink" href="' . esc_attr( $item->url ) .'#'. $sub_field_link .'">';
    		    	$item_output .= $sub_field_value;
    		    	$item_output .= '</a></div>';
     			}
    		}
    
    		$item_output .= $args->after;
    		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    		self::$count++;
    	}
    
    	function start_lvl( &$output, $depth = 0, $args = array() ) {
            $indent = str_repeat("\t", $depth);
            $output .= "\n$indent<ul class='sub-menu'><div class='wrapper clearfix'>\n";
        }
        function end_lvl( &$output, $depth = 0, $args = array() ) {
            $indent = str_repeat("\t", $depth);
            $output .= "$indent</div></ul>\n";
        }
    }
  • Hi @teun

    That’s because your code only try to get the second row:

    $meta_key = 'flexibele_content_1_menukaart-repeater_'.$i.'_categorie';

    Please notice the index “1” in your code. Because the index starts from “0”, if you set it to “1” you will only get the second row.

    Could you please let me know why you can’t use ACF functions to get the flexible content value? This page should give you more idea about it: https://www.advancedcustomfields.com/resources/flexible-content/.

    Thanks 🙂

  • Ugh, I was in the assumption that that wouldn’t work in a walker, but I just tried a simple custom field and it works without problems.

    I’ll try to fix it this way, thanks! 🙂

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

The topic ‘get_post_meta for a repeater field in a flexible content field?’ is closed to new replies.