Support

Account

Home Forums Front-end Issues Relationship Field Objects Not Showing

Solved

Relationship Field Objects Not Showing

  • So here’s a weird problem. I just updated a site’s plugins (hasn’t been updated in a few months), which included updating ACF to 4.3.

    A relationship field on the site is set to output post objects. When logged in, everything works fine, but when logged out, the relationship field doesn’t output anything on the front-end. I am even calling get_fields which shows that all other custom fields on the page work. The output for the relationship field is an empty array. Changing the relationship field to output post IDs allows it to work. Weird.

    If you need more data, I know that this was also an issue with 4.2.2. Uploading an archived database allows the site to work fine. So just changing the database, not the files, is the difference between the relationship field outputting post objects or not. Resaving the post and the fields under both databases didn’t make a difference. The field works correctly in the backend and as I said, does work on the front end, ONLY if the user is logged in.

    • Elliot

    • November 9, 2013 at 10:17 am

    Hi @david Crabill

    Is it possible that you have a caching plugin which could be causing the issue between logged in / logged out?

    Also, do you have a plugin which would hook into the get_post or get_posts function and remove results based on the logged in status of the current user?

    Thanks
    E

  • Thanks for responding. It’s not a caching issue… it occurred in all browsers, in incognito, and on the live server and localhost.

    I have temporarily bypassed the issue by getting post IDs and then calling get_post on those IDs… I thought that would indicate that there is not a conflict on my site. However, I should have checked this with a fresh WP install, and now I have and I can see that it works there. So it’s something site-specific… sorry about that, please disregard.

    • Elliot

    • November 9, 2013 at 11:04 am

    Hi @david Crabill

    Thanks for following up!

    Cheers
    E

  • Hi,

    We’re having the same issue. The posts which are related to the page are showing in the front-end when we’re logged in, but once we log out, they aren’t appearing.

    Very strange.

    John

    • Elliot

    • February 21, 2014 at 10:28 am

    Hi @john Wilson

    Sounds like either a user role plugin is preventing data from being loaded for non logged in users, or the pages are cached and need to be emptied.

    Thanks
    E

  • Hi Elliot,

    Upon investigation, it appears that it’s bbPress which it doesn’t like. Which is a problem for us, as we need to forum to be working on the site. Going to have to look into it further.

    Thanks,
    John

  • Good catch @john Wilson. We’re definitely running bbPress too.

  • Hi Elliot,

    Have you had chance to have a look at or heard of a similar issue with bbPress & the relationship field?

    Cheers,
    John

  • A bit of investigation and it seems it’s also the Post Object field which has this issue.

    I also managed to fix this by changing the Return Format for the field to Post ID rather than Post Object. Not ideal but it’s a solution.

  • I think i figured out this problem with this.

    It all boils down to WordPress’s get_posts() function and some of the names used for custom post types by bbpress.

    bbpress uses ‘reply’ and ‘topic’, they probably should be namespaced. When ACF calls get_posts() inside the relationship code it uses the ‘acf/get_post_types’ filter to retrieve all post types setup for the them. It then passes in those post types as an array to the WordPress get_posts() function as the ‘post_type’ parameter.

    WordPress does not like either ‘reply’ or ‘topic’ being passed in there. WordPress will only give you the results when logged in. When logged out if either of those names are passed into get_posts via the post_type parameter you get nothing back.

    So really this is either something that needs to be fixed with core WordPress or bbpress should change the name of their post types.

    But until that happens people that use both plugins in their theme can chuck something like this in their functions file:-

    add_filter('acf/get_post_types', 'my_acf_post_type_filter', 20, 3);
    
    function my_acf_post_type_filter ($post_types) {
        $exclude = array('topic', 'reply');
    
        // exclude
        foreach( $exclude as $p )
        {
            unset( $post_types[ $p ] );
        }
    
        return $post_types;
    }

    The other option would be for Elliot to just stick those two post type names as things to exclude in his get_post_types() function in the core of ACF to just sidestep this issue altogether.

  • Still having this issue even after adding the code above to my functions file.

    //edit – must not have hit refresh hard enough. This solution is working fine.

    • mchl

    • July 31, 2014 at 8:32 pm

    Just came across this issue a week after deploying the site! Went undetected during dev as i was logged in the whole time. Thanks heaps for the above fix, works perfectly.

  • Just to follow up, this appears to be working in ACF 5 without any custom code needed.

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

The topic ‘Relationship Field Objects Not Showing’ is closed to new replies.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.