Reputation: 595
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
) );
.......
Upvotes: 0
Views: 490
Reputation: 146191
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 );
Upvotes: 2
Reputation: 1809
I am not much aware of wordpress but can help you logically. As per my knowledge, This can be done by checking condition like "WHERE creation_date < 8 months" in your query while retriving posts for category 'older news'. So first get current date.
<?php $today = date('Y-m-d'); ?>
Then subtract 8 months from it and you will get a date 8 month older
<?php $eight_month = date('Y-m-d',strtotime("-8 month $today")); ?>
now you can use $eight_month in your query so your query would be something like this. Check format of date you are storing in your database for post and change my accordingly if it is different than format "Y-m-d"
"SELECT * FROM posts WHERE creation_date < '$eight_month'"
Hope this is what you want and help you in your project. Best luck and have a nice day
Upvotes: 0