Home › Forums › General Issues › Notice: Trying to get property of non-object Relationship field
I’m getting an error when using 2 relationship fields on the same page, on a wordpress multi site (I don’t know if these points are causing the error)
Notice: Trying to get property of non-object in acf.php line 218
I’ve updated the line to
if (is_object($autosave)) {
if( $autosave->post_parent == $post_id ){
$post_id = intval( $autosave->ID );
}
}
Which appears to resolve the issue, thought I’d share incase it helps anyone else.
Hi @agilepixel
Thanks for the bug report.
The code in question is wrapped in a conditional statement testing if a $_GET parameter exists for a preview.
Is your error in the backend or front-end of the website?
The error was occurring on the front end, within a custom template.
Hi @agilepixel
If the issue is related to a piece of code in your template, please paste your code so I can see what is triggering the issue
Sure this is my templates’s Code.
Thanks for taking the time to look at this.
<div class="content__home content_page">
<div class="header__slider flexslider">
<ul class="slides">
<?php if(get_field('home_page_carousel_rep')): ?>
<?php while(has_sub_field('home_page_carousel_rep')): ?>
<li>
<?php if(get_sub_field('home_page_carousel_link')): ?>
<a href="<?php echo get_sub_field('home_page_carousel_link'); ?>">
<?php endif; ?>
<?php if(get_sub_field('home_page_carousel_image')): ?>
<?php $image = get_sub_field('home_page_carousel_image'); ?>
<img src="<?php echo $image['url'] ; ?>" alt="<?php echo $image['alt'] ; ?>" title="<?php echo $image['title'] ; ?>" />
<?php endif; ?>
<?php if(get_sub_field('home_page_carousel_link')): ?>
</a>
<?php endif; ?>
</li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div>
<div class="internal__pages grid">
<?php
$page_chooser = get_field('home_page_chooser_rep');
if( $page_chooser ):
foreach( $page_chooser as $post): ?><?php setup_postdata($post); ?><div class="internal__pages__item grid__item one-quarter">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?></a>
</div><!--
--><?php endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
</div>
<hr />
<div class="grid two__col">
<?php
$home_page_quick_links = get_field('home_page_quick_links');
if( $home_page_quick_links ):
foreach( $home_page_quick_links as $post): ?><?php setup_postdata($post); ?><div class="grid__item one-half palm-one-whole">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?></a>
<h2 class="single__title"><?php the_title(); ?></h2>
</div><!--
--><?php endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
</div>
<hr />
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<nav class="pagination">', 'after' => '</nav>')); ?>
<?php endwhile; ?>
</div>
Hi @agilepixel
Can you try removing blocks of code from your template until the issue goes away?
At what line does the issue start?
This will show exactly what the issue is coming from.
Also, are you using the setup_postdata on an autosave post or non published post?
I got the same error using a relationship field. I removed blocks from my code until the errors going away. The setup_postdata($post)
causes this error but the code is still working and shows the_title()
or the_permalink()
.
That surprises me because in the example code it says “This example uses a function called setup_postdata which will override the global $post object and allow functions such as the_title to target the selected post.”
Hope this helps
Thomas
The topic ‘Notice: Trying to get property of non-object Relationship field’ 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.