0

I need to get posts from a certain timeframe. I am using the posts_where filter.

I want to specify a beginning date and an end date. This is the function I use:

function filter_where( $where = '' ) {
    $where .= " AND post_date >= '2012-03-01' AND post_date =< '2014-03-01'";
    return $where;
}

This Filter returns nothing. The error ust be located anywhere around the second AND.

This is my function:

number_sold_mottos( $from, $till );

function number_sold_mottos( $from, $till ){
    add_filter( 'posts_where', 'filter_where' );
    $args = array( 'posts_per_page' => -1, 'post_type' => 'shop_order'  );
    $post_ids   = array();
    $the_query = new WP_Query( $args );
    $last_order_ids = array();
    if ( $the_query -> have_posts() ):
        while ( $the_query -> have_posts()) :
            $the_query -> the_post();
            the_title();
        endwhile;
    endif;
    wp_reset_query();
    remove_filter( 'posts_where', 'filter_where' ); 
}


function filter_where( $where ) {
    $where .= " AND post_date >= '2012-03-01' AND post_date =< '2014-03-01'";
    return $where;
}

Any help is welcome.

  • could you show the code for your loop too please, are you using query_posts? – Matt.C Sep 17 '13 at 16:48
  • 1
    Your only problem was that =< operator doen't exists in MySQL, the right operator for 'lower-or-equal-to' is <= – gmazzap Sep 17 '13 at 17:10
  • hmm, didn't spot that! – Matt.C Sep 17 '13 at 17:18
0

You should probably add the filter, then use a new query, then remove the filter.

function filter_where( $where ) {
    $where .= " AND post_date >= '2012-03-01' AND post_date <= '2014-03-01'";
    return $where;
}
add_filter( 'posts_where', 'filter_where' );

$query = new WP_Query( array( 'post_type' => 'shop_order' ) );
while( $query->have_posts() ) : $query->the_post();
    the_title();
    the_content();
endwhile;

remove_filter( 'posts_where', 'filter_where' );
  • please have a look at my edit. – user998163 Sep 17 '13 at 17:01
  • does this code work if you use it in place of your function above? – Matt.C Sep 17 '13 at 17:07
0

Maybe this can help you:

//Add the filter
add_filter('posts_where', 'filter_where' );
//Remove the = ''
function filter_where( $where ) {
    $where .= " AND post_date >= '2012-03-01' AND post_date =< '2014-03-01'";
    return $where;
}

You must know also that this filter will affect to all the queries performed in your site except the queries using get_posts() function, except if you set the argument suppress_filters to false, for example get_posts( array( 'suppress_filters' => FALSE ) );

0

don't do empty $where variable in function argument this is why you are getting nothing, because your $where statement defined in your function has not any query parameters other than date range, you need to expand $where like this.

EDITED:

number_sold_mottos( $from, $till );

function number_sold_mottos( $from, $till ){
     add_filter( 'posts_where', 'filter_where' );
     $args = array( 'posts_per_page' => -1, 'post_type' => 'shop_order'  );
     $post_ids   = array();
     $the_query = new WP_Query( $args );
     $last_order_ids = array();
     if ( $the_query->have_posts() ):
         while ( $the_query->have_posts()) :
             $the_query->the_post();
             the_title();
         endwhile;
         wp_reset_postdata();
     endif;
    remove_filter( 'posts_where', 'filter_where' ); 
}


function filter_where( $where ) {
    $where .= " AND post_date >= '2012-03-01' AND post_date <= '2014-03-01'";
    return $where;
}
  • I tried it your way, but I still do not get any result. – user998163 Sep 17 '13 at 16:53
  • @user998163 i have edited your code try now. – Anjum Sep 17 '13 at 17:06
0

Additional information (no solution before WordPress 3.7): There is WP_Date_Query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.