andytc
andytc

Reputation: 29

Else no posts found in foreach loop with while

This is on a CPT archive with Taxonomy terms , i can't get the 'No posts found' to show up ? the loop works , it displays each term name as a heading and shows the posts under each term in a list. I have tried a lot of code to get the 'No posts found' to show up ... nothing is working ?

<?php


//fetch the terms for the policy taxonomy
  $terms = get_terms( 'policy-groups', array(
  'hide_empty' => 'true',
) );

// run a query for each policy-group term
foreach( $terms as $term ) :
   $args = array(
    'post_type'  => 'policies',
    'policy-groups' => $term->slug ,
    'order'        => 'DSC',
    'posts_per_page' => -1,
);

  $query = new WP_Query( $args );  
     if( $query->have_posts() ) :  ?>

       <ul>

         <?php
         // output the policy-group name in a heading tag               
         echo'<h4 class="policy-archive-heading">' . $term->name . '</h4>'; 

         // Start while the Loop   
          while ( $query->have_posts() ) : $query->the_post(); 
             $attachment_id = get_field( "policy_upload" );
             $url = wp_get_attachment_url( $attachment_id );
          ?>

            <li>
                <a href="<?php echo $url; ?>" target="_blank"><?php the_title(  ); ?></a>
            </li>  


<?php endwhile; //endwhile ?>

<?php else: printf( __('<strong>Sorry , No posts were found</strong>')); ?>

<?php endif; //end if posts ?>

</ul>

<?php endforeach ?>


// use reset postdata to restore orginal query
wp_reset_postdata();

Upvotes: 0

Views: 1067

Answers (2)

Prateek Verma
Prateek Verma

Reputation: 889

You are not displaying the terms which have no posts. For showing the terms having no posts, change the 'hide_empty' => 'false' in your code.

Please find your updated code of $terms query:

$terms = get_terms( 'policy-groups', array(
  'hide_empty' => false,
) );

And also correct your $args query, please find the code below:

$args = array(
        'post_type'  => 'policies',
        'post_status' => 'publish',
        'tax-query'   => array(
              array( 
                 'taxonomy' => 'policy-groups',
                 'field' => 'slug',
                 'term' => $term->slug,
              )
         ),
        'orderby'     =>  'title',
        'order'        => 'ASC',
        'posts_per_page' => -1,
    );

And also place the opening '<ul>' tag before the if( $query->have_posts() ) condition or place the closing '</ul>' just after 'endwhile;', as they are not properly synchronized.

After all the correction, here is your full code, copy & replace with your code:

<?php
//fetch the terms for the policy taxonomy
  $terms = get_terms( 'policy-groups', array(
  'hide_empty' => false,
) );

// run a query for each policy-group term
foreach( $terms as $term ) :
   $args = array(
        'post_type'  => 'policies',
        'post_status' => 'publish',
        'tax-query'   => array(
              array( 
                 'taxonomy' => 'policy-groups',
                 'field' => 'slug',
                 'term' => $term->slug,
              )
         ),
        'orderby'     =>  'title',
        'order'        => 'ASC',
        'posts_per_page' => -1,
    );

    $query = new WP_Query( $args );  ?>
    <ul>
        <?php if( $query->have_posts() ) :  
            // output the policy-group name in a heading tag               
            echo'<h4 class="policy-archive-heading">' . $term->name . '</h4>'; 

            // Start while the Loop   
            while ( $query->have_posts() ) : $query->the_post(); 
                $attachment_id = get_field( "policy_upload" );
                $url = wp_get_attachment_url( $attachment_id ); ?>
                <li>
                    <a href="<?php echo $url; ?>" target="_blank"><?php the_title(  ); ?></a>
                </li>  


            <?php endwhile; //endwhile ?>

        <?php else: printf( __('<strong>Sorry , No posts were found</strong>')); ?>

        <?php endif; //end if posts ?>

    </ul>

<?php endforeach ;
// use reset postdata to restore orginal query
wp_reset_postdata(); ?>

Please change the 'order' => 'ASC/DESC' as per your requirement in the above queries.

I hope, it may be helpful to you.

Upvotes: 1

chifliiiii
chifliiiii

Reputation: 2359

I would change a couple of things:

  • Search by term id
  • Only publish posts
  • Move reset postdata to right location
  • Typo in DESC order

Code below:

 <?php


    //fetch the terms for the policy taxonomy
      $terms = get_terms( 'policy-groups', array(
      'hide_empty' => 'true',
    ) );

    // run a query for each policy-group term
    foreach( $terms as $term ) :
       $args = array(
        'post_type'  => 'policies',
        'post_status' => 'publish',
        'tax-query'   => array(
              array( 
                 'taxonomy' => 'policy-groups',
                 'term' => $term->term_id
              )

        ),
        'order'        => 'DSC',
        'posts_per_page' => -1,
    );

      $query = new WP_Query( $args );  
         if( $query->have_posts() ) :  ?>

           <ul>

             <?php
             // output the policy-group name in a heading tag               
             echo'<h4 class="policy-archive-heading">' . $term->name . '</h4>'; 

             // Start while the Loop   
              while ( $query->have_posts() ) : $query->the_post(); 
                 $attachment_id = get_field( "policy_upload" );
                 $url = wp_get_attachment_url( $attachment_id );
              ?>

                <li>
                    <a href="<?php echo $url; ?>" target="_blank"><?php the_title(  ); ?></a>
                </li>  


    <?php endwhile; //endwhile ?>

    <?php else: printf( __('<strong>Sorry , No posts were found</strong>')); ?>

    <?php endif; //end if posts ?>

    </ul>

    <?php 
      // use reset postdata to restore orginal query
      wp_reset_postdata();
         endforeach; ?>

Upvotes: 0

Related Questions