Home › Forums › Backend Issues (wp-admin) › Invalid Array from Relationship Field Type
I’m trying to fix a big that seems hard-coded into our wordpress theme’s use of ACF. This was done long ago, but I’m newly aboard to rethink about a site redesign.
We have posts whose authors are user-defined by an ACF relationship field to another custom post type. The following code returns an error that some element of this is an invalid format for a foreach() array. I’ll confess I’m out of my depth here, and I’m hoping what’s wrong is obvious enough to be easily fixed!
Here’s what the code looks like. Line 23 is what returns the error, presumably any time one of our posts has multiple “persons” posts related (which is nearly every time, I’m afraid).
21 $current_post_id = get_queried_object_id();
22 $acfield = get_field('persons', $current_post_id);
23 foreach( $acfield as $post){
24 setup_postdata($post);
25 ?><meta name="author" content="<?php the_title() ?>" /><?php
26 }
27 wp_reset_postdata();
28 if(! $acfield){
29 if (is_single()){
30 global $post;
31 $author = get_the_author_meta('display_name', $post->post_author);
32 echo "<meta name=\"author\" content=\"$author\">";
33 }
34 }
Thanks for your help, let me know what else I can provide that might clue me into a fix!
Just want to get a better understanding of where this is running.
Is this inside the main posts loop? have_posts()
is this in an archive template or a single template? The reason I’m asking is that there is a check for is_single()
which would not be needed on a single post page. So what template file this is running it would make a difference on why there’s an error.
Thanks so much for your reply.
The errors that are thrown back with invalid argument relate to three uses of this code.
On our site’s homepage, the latest post is shown and uses a version of this code from a loop-index.php. On the homepage there is also a use by a postslider.php insert that shows other recent content. Then on each post the code is used by the header.php. So I suppose the answer is both? Site is religiousstudiesproject.com/
I believe the check for is_single() makes sure that if an editor/user did not specify a ‘person’ in content creation that their username appears as the author. Otherwise this code uses the ACF relationship field supplied ‘persons’ for authorship.
I tried several things this morning, including changing the construction of the $acfield variable from $acfield = get_field('persons', $current_post_id);
to $acfield = get_field('persons');
since I wondered whether it was necessary to specific $current_post_id when the get_field was already returning a post id by default.
I then saw an old support ticket suggesting the $acfield’s construction needed to be inside the foreach(). That didn’t help either.
Neither of those changes broke the code, nor did they stop the errors. I simply don’t understand enough about why or how the argument is invalid to move forward with a solution.
[Sidebar: I’m redoing the site and hope to fix the source of these custom php errors by using ACF, elementor and a dynamic content plugin that will give us greater flexibility on the appearance of this part of our site]
Thanks so much for your reply.
The errors that are thrown back with invalid argument relate to three uses of this code.
On our site’s homepage, the latest post is shown and uses a version of this code from a loop-index.php. On the homepage there is also a use by a postslider.php insert that shows other recent content. Then on each post the code is used by the header.php. So I suppose the answer is both? Site is religiousstudiesproject.com
I believe the check for is_single() makes sure that if an editor/user did not specify a ‘person’ in content creation that their username appears as the author. Otherwise this code uses the ACF relationship field supplied ‘persons’ for authorship.
I tried several things this morning, including changing the construction of the $acfield variable from $acfield = get_field('persons', $current_post_id);
to $acfield = get_field('persons');
since I wondered whether it was necessary to specific $current_post_id when the get_field was already returning a post id by default.
I then saw an old support ticket suggesting the $acfield’s construction needed to be inside the foreach(). That didn’t help either.
Neither of those changes broke the code, nor did they stop the errors. I simply don’t understand enough about why or how the argument is invalid to move forward with a solution.
[Sidebar: I’m redoing the site and hope to fix the source of these custom php errors by using ACF, elementor and a dynamic content plugin that will give us greater flexibility on the appearance of this part of our site]
Thanks so much for your reply.
The errors that are thrown back with invalid argument relate to three uses of this code.
On our site’s homepage, the latest post is shown and uses a version of this code from a loop-index.php. On the homepage there is also a use by a postslider.php insert that shows other recent content. Then on each post the code is used by the header.php. So I suppose the answer is both? Site is religiousstudiesproject.com
I believe the check for is_single() makes sure that if an editor/user did not specify a ‘person’ in content creation that their username appears as the author. Otherwise this code uses the ACF relationship field supplied ‘persons’ for authorship.
I tried several things this morning, including changing the construction of the $acfield variable from $acfield = get_field('persons', $current_post_id);
to $acfield = get_field('persons');
since I wondered whether it was necessary to specific $current_post_id when the get_field was already returning a post id by default.
I then saw an old support ticket suggesting the $acfield’s construction needed to be inside the foreach(). That didn’t help either.
Neither of those changes broke the code, nor did they stop the errors. I simply don’t understand enough about why or how the argument is invalid to move forward with a solution.
[Sidebar: I’m redoing the site and hope to fix the source of these custom php errors by using ACF, elementor and a dynamic content plugin that will give us greater flexibility on the appearance of this part of our site]
// use get_queried_object because you might not be showing
// something that returns an value for get_queried_object_id()
$queried_object = get_queried_object();
// initialize $persons in case nothing is returnd for field
$persons = false;
// test queried object to see if it's a post
if (is_a($queried_object, 'WP_POST')) {
$persons = get_field('persons', $queried_object->ID);
}
// test $persons to make sure something was returned
if (!empty($persons)) {
foreach ($persons as $post) {
setup_postdata($post);
?><meta name="author" content="<?php the_title() ?>" /><?php
}
wp_reset_postdata();
}
// if nothing was returned for $persons show a default
// if this is a single post
if (!$persons && is_single()) {
?><meta name="author" content="<?php echo get_the_author_meta('display_name', $post->post_author); ?>"><?php
}
The topic ‘Invalid Array from Relationship Field Type’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.