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
Or simply