Support

Account

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

  • Hi John, thank you for your advice.
    For some reason filter is not working when it’s added to the page. Definitely works when add_action is being added to the functions.php, but it’s preventing me from seeing any drafts on the site.

    
    <?php
    if (get_field('alternative_method')):
    	// add filter to only get published posts
    	add_action('pre_get_posts', 'acf_field_only_published_posts', 10, 1);
    	// 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)): ?>
    
    	<section id="alt-method">
          <div class="text full">
            <h2>Alternative methods</h2>
          </div>
    
          <div class="recipes-list item-listing">
              <?php foreach( $alt_posts as $post ):
                  setup_postdata($post); ?>
                  <?php if ( get_post_status() == 'publish' ) :?>
                     <?php get_template_part( 'template-parts/content-recipe-card', get_post_format() ); ?>
                  <?php endif;?>
              <?php endforeach; ?>
              <?php
              wp_reset_postdata(); ?>
    
          </div>
        </section>
    
    	<?php endif; ?>
    <?php endif; ?>