Support

Account

Home Forums Add-ons Repeater Field Repeater field with IF/ELSE based on sub_field value Reply To: Repeater field with IF/ELSE based on sub_field value

  • I’d still like to sort out how to query all the posts for each of these types, merge them together and sort by date…

    This is what I’ve wrote up so far:

    
    <?php
    $my_post_types = array( 'auctions', 'liquidations', 'inventory', 'post' );
    
      $recent_auctions = get_posts( array(
        'post_type' 			=> 'auction',
        'posts_per_page' 	=> get_sub_field('items_per_row'),
        'orderby' 				=> 'date',
        'order' 					=> 'DESC',
        'post_status'     => 'publish',
      ));
      $recent_liquidations = get_posts( array(
        'post_type' 			=> 'liquidation',
        'posts_per_page' 	=> get_sub_field('items_per_row'),
        'orderby' 				=> 'date',
        'order' 					=> 'DESC',
        'post_status'     => 'publish',
      ));
      $recent_inventory = get_posts( array(
        'post_type' 			=> 'inventory',
        'posts_per_page' 	=> get_sub_field('items_per_row'),
        'orderby' 				=> 'date',
        'order' 					=> 'DESC',
        'post_status'     => 'publish',
      ));
      $recent_studies = get_posts( array(
        'post_type' 			=> 'post',
        'cat'   					=> 304 ,
        'posts_per_page' 	=> get_sub_field('items_per_row'),
        'orderby' 				=> 'date',
        'order' 					=> 'DESC',
        'post_status'     => 'publish',
      ));
    
    $mergedposts = array_merge( $recent_auctions, $recent_liquidations, $recent_inventory, $recent_studies ); //combine queries
    $postids = array();
    foreach( $mergedposts as $item ) {
      $postids[]=$item->ID; //create a new query only of the post ids
    }
    $uniqueposts = array_unique($postids); //remove duplicate post ids
    $posts = get_posts(array(
      //new query of only the unique post ids on the merged queries from above
      'post__in' => $uniqueposts,
      'post_type' => $my_post_types,
      'post_status' => 'publish',
    ));
    
    foreach( $posts as $post ) : setup_postdata($post); ?>
        <div class="row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4">
          <?php $type = get_sub_field('content_type'); ?>
    
          <?php if($type == 'auctions'): ?>
              <?php get_template_part( 'template-parts/card', 'auction' ); ?>
    
          <?php elseif($type == 'liquidation'): ?>
              <?php get_template_part( 'template-parts/card', 'liquidation' ); ?>
    
          <?php elseif($type == 'inventory'): ?>
          <?php get_template_part( 'template-parts/card', 'inventory' ); ?>
          
          <?php elseif($type == 'studies'): ?>
          <?php get_template_part( 'template-parts/card', 'studies' ); ?>
    
          <?php endif; // end if ?>
        </div><!-- row -->
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>
    

    Any advice on merging queries like this??