Support

Account

Home Forums ACF PRO If relationship field has only draft posts then hide this section

Solving

If relationship field has only draft posts then hide this section

  • Hi guys, I’ve been wondering if there is an efficient way to hide sections if all of the posts listed in the relationship field are being set as drafts.
    Is there a way to check if posts are being published? I’m trying to hide the section below altogether if there are no published posts.

    
    <?php if( get_field('alternative_method') ): ?>
    
    <section id="alt-method">
          <div class="text full">
            <h2>Alternative methods</h2>
          </div>
    
          <div class="recipes-list item-listing">
    
          <?php
          $alt_posts = get_field('alternative_method');
          if( $alt_posts ): ?>
              <?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(); ?>
          <?php endif; ?>
    
          </div>
        </section>
    
    <?php endif; ?>
    
  • 
    <?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')
    
  • 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; ?>
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.