0

I am using WP Query for a custom taxonomy–"featured layout"–and have two scenarios I'm trying to work out: either display the most recent Featured Layout that has a particular value set using Advanced Custom Fields OR display the most recent Featured Layout post (i.e. $stickyToggle == yes. The former would take precedence.

Right now, the following code only outputs the most recent post but I can't figure how to first check and output the post if it's both a Featured Layout and $stickyToggle = yes, and if it's not, just output the most recent Featured Layout. Here is the code:

    <?php
    //Getting 'Featured' Value
    $featured_value_cat_query = new WP_Query( array (
      'post_type' => 'seacoast_values',
      'posts_per_page' => -1,
      'tax_query' => array(
          'relation' => 'AND',
          array(
            'taxonomy' => 'seacoast_value_category',
            'field'    => 'slug',
            //$term_slug is main category/taxonomy term
            'terms'    => $term_slug,
          ),
          array(
            'taxonomy' => 'seacoast_value_layout_position',
            'field'    => 'slug',
            'terms'    => 'featured-layout',
            'orderby'  => 'date',
            'order'    => 'DESC',
          ),
      ),
    ) );

    $stickyToggle = get_field('seacoast_value_sticky_value');

    //Returning 'Featured'

    if ( $featured_value_cat_query->have_posts() ) {

      while ( $featured_value_cat_query->have_posts() ): $featured_value_cat_query->the_post(); ?>

    Post Content would be here

  <?php

    endwhile;  //end of 'Featured' loop

    //* Restore original Post Data
    wp_reset_postdata();

  } //end of if post ?>
0

Run through the loop once and break when the sticky post is found. If it's not found, rewind the query and output the first found result:

<?php if ( $featured_value_cat_query->have_posts() ) {
   $count = 0;

   while ( $featured_value_cat_query->have_posts() ): 
      $featured_value_cat_query->the_post();

      if ( 'yes' == get_field('seacoast_value_sticky_value') )
      { 
         $count = 1; ?>

         // Post Content would be here

         <?php break;
      }

   endwhile;  //end of 'Featured' loop

   if ($count == 0) {
      $featured_value_cat_query->rewind_posts();

      while ( $featured_value_cat_query->have_posts() ): 
         $featured_value_cat_query->the_post(); ?>

         // Post Content would be here

         <?php break;

      endwhile;  //end of 'Featured' loop
   }
} //end of if post

//* Restore original Post Data
wp_reset_postdata(); 
?>
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.