$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>advanced custom fields - Getting property of non-object when looping through post objects from ACF|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

advanced custom fields - Getting property of non-object when looping through post objects from ACF

matteradmin8PV0评论

I am building a site using Root's Sage Wordpress theme which uses the blade templating engine.

I have a repeater field in ACF with many post objects. When I foreach loop through the repeater to retrieve the post objects, I am getting a Notice: Trying to get property of non-object errors. When I turn off WP_Debug, these errors are not present and the following code works as expected.

Below is a snippet of the code used:


global $post; 

@foreach($page_column as $pages)


    {{-- If the link is internal --}}
    @if($pages['link_type'] === "Internal")

        @foreach($pages as $post)

            @php 

            setup_postdata($post); 

            @endphp

            <li class="footer__list-item">

                <a class="footer__link" href="{{get_the_permalink($post->ID)}}">{{$post->post_title}}</a>

            </li>

            @php wp_reset_postdata(); @endphp

        @endforeach

    @endif

@endforeach

Below is what is output:

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
Home

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
About

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
Locations

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
Case Studies

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
Downloads

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

What is interesting is you can see the code is working as intended because below most of the error messages, you can see the post title being output. I also do not understand why there are multiple errors thrown per page as the code is retrieving all the pages from the repeater field successfully.

From what I found when looking up the error, I added in the setup_postdata($post) to see if that would fix the issue however that does not seem to have worked.

What am I doing wrong here?

I am building a site using Root's Sage Wordpress theme which uses the blade templating engine.

I have a repeater field in ACF with many post objects. When I foreach loop through the repeater to retrieve the post objects, I am getting a Notice: Trying to get property of non-object errors. When I turn off WP_Debug, these errors are not present and the following code works as expected.

Below is a snippet of the code used:


global $post; 

@foreach($page_column as $pages)


    {{-- If the link is internal --}}
    @if($pages['link_type'] === "Internal")

        @foreach($pages as $post)

            @php 

            setup_postdata($post); 

            @endphp

            <li class="footer__list-item">

                <a class="footer__link" href="{{get_the_permalink($post->ID)}}">{{$post->post_title}}</a>

            </li>

            @php wp_reset_postdata(); @endphp

        @endforeach

    @endif

@endforeach

Below is what is output:

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
Home

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
About

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
Locations

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
Case Studies

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44
Downloads

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

Notice: Trying to get property 'post_title' of non-object in /homepages/6/d142103519/htdocs/development-sites/newable-3.0/wp-content/uploads/cache/0df15fedcc33b7f94d22294aa741cea17fe36f28.php on line 44

What is interesting is you can see the code is working as intended because below most of the error messages, you can see the post title being output. I also do not understand why there are multiple errors thrown per page as the code is retrieving all the pages from the repeater field successfully.

From what I found when looking up the error, I added in the setup_postdata($post) to see if that would fix the issue however that does not seem to have worked.

What am I doing wrong here?

Share Improve this question asked Mar 13, 2019 at 13:07 GazzGazz 1112 bronze badges 2
  • I would turn off the debug! – Elkrat Commented Mar 13, 2019 at 13:14
  • @Elkrat I like to have it on whilst developing. – Gazz Commented Mar 13, 2019 at 13:16
Add a comment  | 

1 Answer 1

Reset to default 1

Nevermind, after a couple of minutes, I figured out the issue.

Instead of passing the $post->ID into get_the_permalink, I can just not pass any parameters in as the setup_postdata($post) takes care of this.

Post a comment

comment list (0)

  1. No comments so far