I noticed that draft posts are available for selection in the Relationship field and I’m wondering if these should be filtered out by default – or perhaps a toggle could be added to the Relationship field to select allowed post statuses (published, draft, private)?
In the absence of a UI for toggling the allowed statuses, rather than using a hook to filter out draft posts, the hook could be used to include them if there was a need for it.
I can see the value of including draft posts: allowing you set up the relationship in advance of publishing said posts or pages. At the same time my gut reaction was that this could introduce additional margin for error for the client/user who is editing the site and also clutters up the UI in cases where the client is averse to clearing out old unpublished posts.
This may not help, just a why does this happen.
When performing a query on the posts ACF does not set a value for post_status. This results in WP using the default value as described here https://developer.wordpress.org/reference/classes/wp_query/#status-parameters.
So basically when you’re logged into the admin you get everything other than “trash”, “inherit” and “auto-draft” statuses.
This differs from the front end as the default there is only “publish” and any custom statuses that have been set up as public.
As far as a change in the way ACF does this, you should contact the developer here https://www.advancedcustomfields.com/contact/
That makes sense! Thanks John.
I’m now wondering if I were to select a draft post when I retrieve the field on the front end will that draft post be included? The main thing that I would worry about is accidentally displaying draft posts on the front end that should not be public
Going to test this today
Because of the default, no. ACF does not set the post_status either, only the “post__in” argument to WP_Query. So WP applies the default of “publish” unless the user is logged in and then it also includes “private”
Good to know! Thanks
I could see this potentially causing some confusion for less savvy users editing a relationship field if they were to select only draft posts (as no posts would appear on the front end)
Was there ever a solve to this? I’m able to remove drafts/pending items from the left hand side of a relationship field via this code:
// Only published posts
function filter_acf_relationship($args, $field, $post_id)
$args[‘post_status’] = ‘publish’;
add_filter(‘acf/fields/relationship/query’, ‘filter_acf_relationship’, 10, 3);
However, I have an upload form for a user on the front-end, and when they submit something it goes straight to draft/pending and they automatically show up in the relationship field as (draft), which is undesirable. How could I remove the draft/pending items from the right-side of the relationship field?
You must be logged in to reply to this topic.
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!
© 2020 Advanced Custom Fields. Subscribe