Is there any function that can let me display older posts and post dates at the bottom of each post?The older posts must be in the same category. I searched on the internet but it seems that everyone just display one previous post only
Thank you very much
The get_posts function allow you to build complex queries for posts, you can use this to bring older post with the order and category parameters.
You can use the wp filters to achieve this
// Return posts from the last 30 days:
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
OR
// Return posts for March 1 to March 15, 2010:
function filter_where( $where = '' ) {
// posts for March 1 to March 15, 2010
$where .= " AND post_date >= '2010-03-01' AND post_date < '2010-03-16'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
see this http://codex.wordpress.org/Class_Reference/WP_Query for more info
Related
I am trying to Group each post in a date-wise order inside a table.
Please check the attached Image
function filter_where( $where = '' ) {
// where post_date > today
$where .= " AND post_date >= '" . date('Y-m-d') . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
Use this code
I am using this query to display my posts:
$today = getdate();
$year=$today["year"];
$month=$today["mon"];
$day=$today["mday"];
query_posts( $query_string.'order=ASC' . '&post.status=future,publish'.'&year='.$year.'&monthnum='.$month );
?>
Now I want to add a filter to only show posts that have been published TODAY or will be published in the future.
Example: Today is the 22nd of March. PostA was published on the 1st of March, PostB has been published today and PostC will be published (status 'future' is already in my query, so it nevertheless will be displayed) on the 24th of March.
My new filter for the query should only display PostB and PostC.
Thanks a lot in advance!
:-)
Try this :
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
$today = date('Y-m-d');
// posts for today and future
$where .= " AND post_date >= $today ";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
Try This:
$archive_year = get_the_time('Y'); $archive_month = get_the_time('m'); $archive_day = get_the_time('d'); query_posts('year=' .$archive_year .'&monthnum=' .$archive_month .'&day=' .$archive_day);
I wish to limit the get_posts() result by dates
i tried using the following but it is not working for me...
can anyone tell me if im doing this correctly ? and why is not performing as expected ?
add_filter( 'posts_where', 'filter_dates_between' );
$args=array(
'post_type'=>'log',
'suppress_filters' => false,
'post_status'=>'private',
'numberposts'=> -1,
'meta_key'=>'_wpcf_belongs_agent_id','meta_value'=>$agentId);
$logs=get_posts($args);
remove_filter( 'posts_where', 'filter_dates_between' );
here is the filter function
function filter_dates_between( $where ='' ) {
global $dateFrom,$dateTo,$wpdb;
$where .= $wpdb->prepare( " AND post_date >= %s", $dateFrom );
$where .= $wpdb->prepare( " AND post_date <= %s", $dateTo );
return $where;
}
Your code is good. I think the problem comes from the dates you pass in which are treated like strings.
Make sure in the end its in this example format
"AND post_date >= '2009-03-01' AND post_date <= '2009-03-15'"
Or simply
"AND cast(post_date as datetime) between cast... AND cast...
I was wondering if there is a way using WP_Query (get_posts, etc) to return posts with an ID greater than one that is provided..
I've been through the WordPress codex and missed any reference to querying posts related to a post ID, if it's even possible without a custom query.
Since it doesn't seep possible to pass it through with the arguments, I've tried writing a method that modifies the posts_where filter but that doesn't seem to work either..
add_filter( 'posts_where', 'filter_since_id');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
// Do Stuff
endwhile;
remove_filter('posts_where' , 'filter_since_id');
...
function filter_since_id($where = ''){
$where .= " AND ID > 3'";
return $where;
}
Clarified so that someone passing through can grab and go:
add_filter( 'posts_where', 'filter_since_id');
function filter_since_id($where = ''){
$where .= " AND ID > 3";
return $where;
}
I can't figure out how to compare timestamps in the WP_query. On the site, classes/events are entered as posts with meta added for the date they begin. In calendar_functions.php, $begtime is defined as the timestamp for the date the class/event starts (not the date the post is created) and $today is defined as today's timestamp. I've tested both of those and they do return correct values.
The below code works fine, it displays a random class/event... except that past classes/events show up. I want to be able to only display items that happen from today on. Its not an option to go in and delete classes/events that have already happened.
Not sure how to handle the >= part.
<?php include('calendar_functions.php'); ?>
<?php $my_query = new WP_Query('"$begtime >= $today"&posts_per_page=1&cat=5,43&orderby=rand');
if (have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>
I will also need to be able to compare two sets of dates, as some classes have two sessions, ex. $begtime >= $today -or- $begtime2 >= $today. ($begtime2 is also already defined).
you need something like this.
function filter_where( $where = '' ) {
// posts 30 to 60 days old
$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters