Support

Account

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

Solving

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??

  • You can just query by all of the post types in question. This will get them all ordered by day. But there isn’t a way to order by post type and date.

    To do this you’d need to get them all ordered by date and then create a usort() to order the $query->posts

    You might also be able to alter the WHERE portion of the query using a posts_where filter.

  • getting all of them is straight forward, except the blog posts that need to pull from cat 304.. I can pull all the CPTs together, OR the blog posts.. I just don’t know how to pull a query that gets all the CPTs and then adds the cat arg for the posts.

    If I can get them all together, sorted by date.. that would be perfect.

  • There isn’t any way to do that with WP_Query directly. This would take a more complex query then WP_Query is capable of.

    To be honest, I’m not even sure I help with the query you’d need because wp_posts is contected to wp_terms thought both wp_term_relationships and wp_term_taxonomy.

    A very simplified form or the WHERE would be

    
    WHERE (post_type = 'post' AND 
           term_id = 304) // this is completely wrong
           OR (post_type IN(your other post types))
    
Viewing 4 posts - 26 through 29 (of 29 total)

You must be logged in to reply to this topic.