Reputation: 59
I want to achieve something like this, I don't know if it is possible and what would be the best way to do it:
http://oi60.tinypic.com/21dokqu.jpg
The way I query the posts is like this:
<div class="post">
<?php global $wp_query;
query_posts( array('post_type' => array( 'lecturas-post' ),'showposts' => 15, 'paged'=>$paged, 'order' => 'DESC' ));?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div><?php the_title() ?></a>
<?php endwhile; // end of the loop. ?>
</div>
Anyone can give me a tip on how or best way to do it?
This is what I tried but it doesn't display anything:
global $wpdb; // Don't forget
$collection = $wpdb->get_results("
SELECT YEAR(p.post_date) AS post_year, MONTH(p.post_date) AS post_month, p.*
FROM {$wpdb->posts} AS p
WHERE p.post_type = 'post' AND p.post_status = 'publish'
ORDER BY p.post_date DESC
", OBJECT );
// Loop once to grab the years
foreach ( $collection as $year ):
// Loop for a second time to grab the months inside a year
foreach ( $collection as $month ):
// Continue unless years match
if ( $month->post_year != $year ) continue;
// Loop one more time to grab the posts inside the month, inside the year
foreach ( $collection as $post ):
// Continue unless months and years match
if ( $post->post_year != $year || $post->post_month != $month ) continue;
// You can use the posts data here
endforeach;
endforeach;
endforeach;
Upvotes: 1
Views: 7640
Reputation: 5824
try this i hope this is working for you.
$args = array('posts_per_page' => -1, 'orderby' => 'date' );
$myQuery = new WP_Query($args);
$date = '';
if ( $myQuery->have_posts() ) : while ( $myQuery->have_posts() ) : $myQuery->the_post();
if ( $date != get_the_date() ) {
echo $date;
echo '<hr />';
$date = get_the_date();
}
the_title(); // or whatever you want here.
echo '<br />';
endwhile; endif;
wp_reset_postdata();
More info on the query here: http://codex.wordpress.org/Class_Reference/WP_Query
i find that answer from here. Group posts by date in Wordpress
answer giving by mikevoermans
Upvotes: 1
Reputation: 21
Hope this will help:
$args = array(
'post_type' => 'lecturas-post',
'numberposts' => 15,
'orderby' => 'post_date',
'order' => 'DESC',
);
$posts = wp_get_recent_posts( $args );
$collection = array();
foreach ( $posts as $lectura ){
$index = date( 'Y-m', strtotime($lectura['post_date']) );
$collection[ $index ][] = $lectura;
}
$collection now contains your posts per month, you can easily loop trough them.
Upvotes: 2