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.

  • 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);
      }
    }
  • 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

  • This saved my life. All my ACF went crazy and stopped existing unless i manually update each post. I had to use this to display the data from the database.

    Now i need guidance for the gallery field (wishful thinking). =S

  • The db for a gallery field contains an array of attachment ID values.

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

You must be logged in to reply to this topic.