Home › Forums › ACF PRO › If relationship field has only draft posts then hide this section › Reply To: If relationship field has only draft posts then hide this section
<?php
// add to function.php
function acf_field_only_published_posts($query) {
$query->set('post_status', 'publish');
}
?>
if (get_field('alternative_method')) {
// add filter to only get published posts
add_action('pre_get_posts', 'acf_field_only_published_posts');
// get field
$alt_posts = get_field('alternative_method');
// remove the filter so it does not affect other queries
remove_filter('pre_get_posts', 'acf_field_only_published_posts');
if (!empty($alt_posts)) {
// the rest of code to output section here
} // end if !empty($alt_posts)
} // end if get_field('alternative_method')
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.