Home Forums ACF PRO How to remove drafted posts from already selected relationship


How to remove drafted posts from already selected relationship

  • Hi! I’m used ACF PRO on many my projects!

    We often use custom fields for job postings. We have 2 custom post types – Jobs and Contact person. Both connected by relationship and on Job post we select Contact peson.
    Also we have functionality which send to draft job post if Job date is expired.

    On functions.php i add also functionality which remove possibility to select drafted post in relationship globally. Like this:

    add_filter( 'acf/fields/relationship/query','relationship_options_filter', 10, 3);
    function relationship_options_filter($options, $field, $the_post) {
    	$options['post_status'] = array('publish');
    	return $options;

    My problem is that if we select post and after date is expired we have this post on right side of relationship field, which is bad for coding, because in loop we still have that post.

    On backend i add also checking if post have status “published”. Frontend part is ok, but i wat to hide drafted post on right side of relationship field – see image below. Highlighted part must be hidden!

    Tell me please what is possible!

  • You would have to use an acf/load_field filter or an acf/prepare_field filter.

    In this filter you would have to use the field value $field['value'] which will hold an array of Post IDs. You would use this and do a WP_Query() using the value in the post__in argument and the ‘post_status’ argument set to ‘publish’ and replace the
    field value with the IDs of the posts returned.

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

You must be logged in to reply to this topic.