apply filters on wordpress get_posts

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 ?

Read More
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;
    }

Related posts

Leave a Reply

1 comment

  1. 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...