Support

Account

Home Forums Add-ons Repeater Field get_post_meta for a repeater field

Solving

get_post_meta for a repeater field

  • I have a custom post type that I’m trying to add extra sortable columns to. I have the columns but can’t seem to populate them. In the past I have used get_post_meta but that doesn’t seem to be working. I’ve double and triple checked the names so I know it’s not that. Here is what I have.

    function change_columns( $cols ) {
      $cols = array(
        'cb'       => '<input type="checkbox" />',
        'title' => 'Title',
        'id'      => __( 'ID', 'trans' ),
        'availability' => __( 'Availability', 'trans' ),
        'date' => __( 'Date' )
      );
      return $cols;
    }
    add_filter( "manage_caed_room_posts_columns", "change_columns" );
    
    function custom_columns( $column, $post_id ) {
      switch ( $column ) {
        case "id":
    	$roomid = get_post_meta( $post_id, 'id', true);
    	echo $roomid;
          break;
        case "availability":
    	$availability = get_post_meta( $post_id, 'available', true);
    	echo $availability;
          break;
      }
    }
    
    add_action( "manage_caed_room_custom_column", "custom_columns", 10, 2 );
    
    // Make these columns sortable
    function sortable_columns() {
      return array(
        'id'      => 'id',
        'availability' => 'availability'
      );
    }
    
    add_filter( "manage_edit-caed_room_sortable_columns", "sortable_columns" );
  • when using get_post_meta()

    The field that is your repeater field will return the # of rows in the repeater field.
    What you need to get is the meta value of the sub field meta key and the actual key will look something like
    {repeater-field-name}_{#}_{sub-field} where {#} is the index of the of the row, starting at 0.

    So you need to first get the value from the repeater field, then do a for loop and build the meta_key values and then get each sub field value.

    
    $repeater_value = get_post_meta($post_id, 'repeater_field_name', true);
    if ($repeater_value) {
      for ($i=0; $i<$repeater_value, $i++) {
        $meta_key = 'repeater_field_name_'.$i.'_sub_field_name';
        $sub_field_value = get_post_meta($post_id, $meta_key, true);
      }
    }
    

    what you do from there will depend on what is stored in the sub field.

    • oakward

    • September 18, 2016 at 6:47 am

    John, your solution is right and worked for me, but there is a typo in the for(…) parameters. Since this post is the first to come up in Google for the issue I hope this helps some people:

    $repeater_value = get_post_meta($post_id, 'repeater_field_name', true);
    if ($repeater_value) {
      for ($i=0; $i<$repeater_value;/*<-fixed*/ $i++) {
        $meta_key = 'repeater_field_name_'.$i.'_sub_field_name';
        $sub_field_value = get_post_meta($post_id, $meta_key, true);
      }
    }
    • Paul

    • September 4, 2019 at 11:29 pm

    Any ideas how this would be done with nested repeaters?

    I’ve created a thread here which contains my code:

    https://support.advancedcustomfields.com/forums/topic/nested-repeaters-using-get-post-meta/

    Many thanks

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.