Divide WP_Query posts by date & post type

I have a WP_Query that divides 2 Posts types by date but the functionality is not there. Currently it outputs the dates and posts on the same level like..

-Date
-Post
-Post
-Date
-Post
-Post

What I would like is to wrap each date and its associated posts inside a div, as well as split the two post types for each date into two columns.

Update

I managed to split the posts up by date and wrap them in separate divs, now I just need to figure out how to divide the two CPTs into separate columns under each date

Updated Query

?php
$day_check = '';
$index = new WP_Query( array(
   'orderby'='post_date',
   'post_type' = array('cpt1', 'cpt2'),
   'posts_per_page' =12));
while ( $index-have_posts() ) : $index-the_post();
   $day = get_the_date('j');
   if ($day != $day_check) {
      if ($day_check != '') {
         echo '/div/div'; // close the divs
      }
      echo 'div class="day"div class="date"' . get_the_date() . '/divdiv class="posts"';
   }?
   li class="post"?php the_title(); ?/li
?php $day_check = $day; endwhile; ?
?php if ( $index-have_posts() ) :?                                         
?php endif; ?

Topic date wp-query query Wordpress

Category Web


The post_type isn't really a meta query - therefore, both of these post_types would at least 1 category added/queried in order to get the filter invoked - using plain SQL is still a last resort.

add_filter( 'posts_groupby', 'my_posts_groupby' );

function my_posts_groupby($groupby) {
   global $wpdb;
   $groupby = "{$wpdb->posts}.post_date";
   return $groupby;
}

According to your example (I'd guess one can pass it like that as well):

$args = array(
   'posts_per_page' => 10, 
   'orderby' => 'date',
   'groupby' => 'date', /* unsure about the parameter, should be easy to find out */
   'post_type' => array('type1', 'type2')
);

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.