0

I have a repeatable field in a custom post type that is linked to another custom post type. I want to loop through the repeatable field and then for each field access the data from the linked post type. The first result is returned, but on the second I get the following error:

Fatal error : [] operator not supported for strings.

I tried removing the brackets from my variables like $staff = $coach['team_staff'] but that did not work.

I also tried setting $staff = array(); before the loop and that did not work.

Not sure what I have wrong here:

global $post;

// Get The Staff Members
$coaches = get_post_meta($post->ID, 'repeatable_fields', true);
if ( $coaches ) :
    foreach ( $coaches as $coach ) :
        $staff[] = $coach['team_staff']; 
        $role[] = $coach['team_role'];

        // Loop through each staff member
        foreach( $staff as $index => $staff ) :
            $args = array (
                'post_type' => 'staff', 
                'title' => $staff
            );

            $posts = get_posts( $args );
            foreach ( $posts as $post ) : setup_postdata ( $post );

                // get post meta here

            endforeach;
        endforeach;

    endforeach;
endif;
3

2 Answers 2

1

You need to be careful with your loop variable names. For example change $staff to $staff_member:

global $post;

// Get The Staff Members
$coaches = get_post_meta($post->ID, 'repeatable_fields', true);
if ( $coaches ) :
    foreach ( $coaches as $coach ) :
        $staff[] = $coach['team_staff']; 
        $role[] = $coach['team_role'];

        // Loop through each staff member
        foreach( $staff as $index => $staff_member ) :
            $args = array (
                'post_type' => 'staff', 
                'title' => $staff_member
            );

            $posts = get_posts( $args );
            foreach ( $posts as $post ) : setup_postdata ( $post );

                // get post meta here

            endforeach;
        endforeach;

    endforeach;
endif;

Also ideally you should initialise your arrays, $staff and $role outside the loops:

$staff = [];
$role = [];

Also, it's unclear why you would repeatedly add to the $staff array and loop over it with each iteration of the $coaches array. Consider separating the two foreach loops and run them one after the other:

global $post;

// Get The Staff Members
$coaches = get_post_meta($post->ID, 'repeatable_fields', true);
if ( $coaches ) :
    $staff = [];
    $role = [];

    foreach ( $coaches as $coach ) :
        $staff[] = $coach['team_staff']; 
        $role[] = $coach['team_role'];
    endforeach;

    // Loop through each staff member
    foreach( $staff as $index => $staff_member ) :
        $args = array (
            'post_type' => 'staff', 
            'title' => $staff_member
        );

        $posts = get_posts( $args );
        foreach ( $posts as $post ) : setup_postdata ( $post );

            // get post meta here

        endforeach;
    endforeach;

endif;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I thought I needed my foreach loops to be nested in order to access the data from the previous loop. Now I know.
Of course. The arrays/variables are not scoped to loop constructs, but only to functions, or globally.
0

Your code design is wrong :

  • Your second foreach is nested in the first one and iterates through the array $staff which is filled in the first foreach. I don't know if it's on purpose, what that means that on the first foreach's first loop, the second foreach will iterate through one entry, the one that was put in it two lines before, through two entries on the second loop, etc...
  • Your second foreach iterates through $staff while naming the value it's currently using $staff. Once the second foreach is executed, your array formerly known as $staff is gone, overwritten by it's first entry's value, which happens to be a string.

When the first foreach is doing its second loop, your code is therefore trying to use the index operator - [] - to a string, as the error says.

Try this :

global $post;

// Get The Staff Members
$coaches = get_post_meta($post->ID, 'repeatable_fields', true);
if ( $coaches ) :
    $staffs = $roles = array();
    foreach ( $coaches as $coach ) :
        $staffs[] = $coach['team_staff']; 
        $roles[] = $coach['team_role'];
    }
    // Loop through each staff member
    foreach( $staffs as $index => $staff ) :
        $args = array (
            'post_type' => 'staff', 
            'title' => $staff
        );

        $posts = get_posts( $args );
        foreach ( $posts as $post ) : setup_postdata ( $post );
            // get post meta here
        }
    }
}

I added a plural to the arrays so that it's more obvious that they contain several roles/staffs, since you applied the same rule naming your other array $coaches and having its values named $coach in the foreach.

3 Comments

You were just a couple of minutes behind the other answer, but this was helpful also. Thank you!
@RiotAct The main point is that you got yourself out of trouble.
One more quick question: I was accessing $role later as $role[$index], how will I access that now? Nevermind: I figured it out: $roles[$index];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.