Home › Forums › Feature Requests › Add ability to exclude drafts from Relationship field › Reply To: Add ability to exclude drafts from Relationship field
@nml @v3nt you don’t need an acf function to get this working, although it might be nice to integrate something into the admin its not needed and is fairly easy to do this in your theme with or without using the filter mentioned above.
Here’s how to do without using the filter:
If you wrap the guts of your relationship loop with a conditional using get_post_status() you can exclude or specify at that point. e.g.
<?php
$posts = get_field('relationship_field_name');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<?php if ( get_post_status() == 'publish' ) :?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span>Custom field from $post: <?php the_field('author'); ?></span>
</li>
<?php endif;?>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
If you don’t setup post data in your loop just use this instead:
<?php
if ( get_post_status($p->ID) == 'publish' ){
// loop code
}
?>
ref: https://codex.wordpress.org/Function_Reference/get_post_status
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.