I'm using the following custom query in Wordpress within a for-loop that enables me to create a tabbed archive for the previous 8 months of the custom post type I've created for 'news'.
This works perfectly however I wish to then show every older post under the heading 'Older news. I'm struggling to figure out how to separate the posts that are older than 8 months from the current date at any one time.
Can anyone advise me as to a custom query structure that would enable me to do this.
Here's the code i use to output the CPTs in their relevant containers.
<?php for($i = $this_month-1; $i > $this_month-8; $i--) :
if($i==-1){$m=11;}
if($i==-2){$m=10;}
if($i==-3){$m=9;}
if($i==-4){$m=8;}
if($i==-5){$m=7;}
if($i==-6){$m=6;}
if($i==-7){$m=5;}
if($i==-8){$m=4;}
else{$m=$i;}
query_posts( array(
'post_type' => 'news',
'posts_per_page' => '-1',
'orderby' => 'date',
'order' => 'ASC',
'monthnum'=>$m
) );
.......
You may try this
<?php
add_filter('posts_where', 'filter_where');
query_posts( array('post_type' => 'news', 'posts_per_page' => '-1', 'orderby' => 'date'));
function filter_where($where = ''){
$where .= " AND post_date < '".date('Y-m-d', strtotime('-8 month'))."'";
return $where;
}
remove_filter('posts_where', 'filter_where');
if (have_posts()) :
while (have_posts()) : the_post();
// your code
endwhile;
endif;
wp_reset_query();
?>
Update: May be this can help you.
$args = array(
'post_type' => 'news',
'posts_per_page' => '-1',
'orderby' => 'date',
'year' => date('Y'), // for current year, date('Y')-1 for 1 year ago
'monthnum' => $m, // the month in the loop
'order' => 'ASC'
);
query_posts( $args );