Support

Account

Home Forums Front-end Issues Sub dividing posts in archive based on true/false

Solving

Sub dividing posts in archive based on true/false

  • I’d like to be able to lists the posts with chosen fields on a category archive page, split into two sets, based on a true/false field.

    This is what I’ve got so far, but no results:

    <?php while ( have_posts() ) : the_post(); ?>
            
    <?php $args = array(
      'post_type' => 'posts',
      'posts_per_page' => -1,
      'meta_query' => array(
        array(
          'key' => 'featured',
          'value' => '1',
          'compare' => '==' // not really needed, this is the default
        )
      )
    );
    $the_query = new WP_Query($args); ?>
     <?php 
            if ($the_query->have_posts() ) :
            while ($the_query->have_posts()) :
              $the_query->the_post();
              ?>       
    		<div class="feature_production">
    				<?php 
                        $subtitle = get_field('subtitle');
                    ?>
                    <div class="productionimage">
                    <?php if ( has_post_thumbnail() ) {
                    the_post_thumbnail();
                    } else { ?><a>"><img src="/wp-content/uploads/2020/11/pab-logo-placeholder.png" />" /></a>
                    <?php } ?>
                    </div>
                    <div class="productionintro">
                        <h3 class="title"><a>"><?php the_title(); ?></a></h3>
                        <h4><? if( !empty($subtitle) ): ?><br /><?php the_field('subtitle'); ?><?php endif; ?></h4>
                    </div>
            </div>
            <?php 
    		endwhile;
    	endif; 
    	wp_reset_query(); ?>
            <?php $args = array(
      'post_type' => 'posts',
      'posts_per_page' => -1,
      'meta_query' => array(
        array(
          'key' => 'featured',
          'value' => '1',
          'compare' => '!=' // not really needed, this is the default
        )
      )
    );
    $the_query = new WP_Query($args); ?>
     <?php 
            if ($the_query->have_posts() ) :
            while ($the_query->have_posts()) :
              $the_query->the_post();
              ?>       
    		<div class="feature_production">
    				<?php 
                        $subtitle = get_field('subtitle');
                    ?>
                    <div class="productionimage">
                    <?php if ( has_post_thumbnail() ) {
                    the_post_thumbnail();
                    } else { ?><a>"><img src="/wp-content/uploads/2020/11/pab-logo-placeholder.png" />" /></a>
                    <?php } ?>
                    </div>
                    <div class="productionintro">
                        <h3 class="title"><a>"><?php the_title(); ?></a></h3>
                        <h4><? if( !empty($subtitle) ): ?><br /><?php the_field('subtitle'); ?><?php endif; ?></h4>
                    </div>
            </div>
            <?php 
    		endwhile;
    	endif; 
    	wp_reset_query(); ?>
    <?php endwhile; ?>
  • OK, a couple of observations:

    Why are the queries run inside a default while ( have_posts() ) : the_post(); loop? Remove that loop or move the additional queries outside of it and go from there.

    To debug this I would do a standard query first and var_dump or print_r the return value of the custom field in question to see what values are actually returned. Is it actually a string, as you seem to be testing for? Or is it true/false or 1 or 0 as integers?

    You should use wp_reset_postdata(), not wp_reset_query(), as this is only for use with query_posts(), as far as I understand, which isn’t recommended anyway (source: https://developer.wordpress.org/reference/functions/wp_reset_query/).

    You can probably use a basic query, as described on https://www.advancedcustomfields.com/resources/query-posts-custom-fields/, since you are just querying for one field anyway.

  • Instead of doing two new queries I would us a pre_get_posts filter

    
    add_action('pre_get_posts', 'my_pre_get_posts');
    function my_pre_get_posts($query) {
      if (!$query->is_main_query() || is_admin()) {
        // return
      }
      // other checks to make sure your altering the correct query
    
      $query->set('posts_per_page', -1);
      $query->set('meta_key', 'featured');
      $query->set('orderby', array('meta_value_num' => 'DESC', 'date' => 'DESC');
    }
    

    The above alterations to the query will sort them with featured post 1st. Then in your loop you need to look at what the field value of the post is and do your separation when it changes from 1 to 0.

  • Thanks both. Will get back to it and see what works!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.