0

I need to select posts for two different months (current, past). As far as i know, it is not possible to pass m date parameter as an array of integer instead of an integer, so I am performing two different queries and getting all posts returned into the same array.

The question is, how do i reorder the result pool of posts? Just pushing the second pool into the array returned by the first query isn't enough, as the orderby argument finds a discontinuity in the joint of the two arrays.

I tried using usort but I get an error saying that You can't use WP_Post object as an array.

Any ideas?

Thanks,

7
  • To clarify, you want posts that were posted in this month, and last month? Commented Oct 1, 2013 at 10:26
  • Yes, both months, say, today it is a date interval of 31 days, by the end of October the interval will span to almost 60 days.
    – versvs
    Commented Oct 1, 2013 at 10:28
  • I found solutions for "get last 30 days posts" but all seem to apply a filter where, modifying query string. I'm querying using an array, and I found that the date parameter specifying month was a better solution. Also, the date_query implemented for WP 3.7 doesnt fit for this project, that is using latest stable release to date (3.6.1)
    – versvs
    Commented Oct 1, 2013 at 10:30
  • Nothing wrong with modifying the query string... (and 3.7 hopefully won't be long :)) Commented Oct 1, 2013 at 10:50
  • but @StephenHarris, I'm preparing my query for get_posts setting query parameters as an associative array. How do I apply the WHERE filter in this case?
    – versvs
    Commented Oct 1, 2013 at 10:52

1 Answer 1

1

This is how I would approach it:

Code

function my_pseudo_date_query( $where, $query ) {

    global $wpdb;

    $dq = $query->get('my_date_query');

    if( $dq && !empty( $dq['after'] ) ){

        $where .=  $wpdb->prepare( 
                      " AND {$wpdb->posts}.post_date >= %s"
                      $dq['after']
                   );
    }

    if( $dq && !empty( $dq['before'] ) ){

        $where .=  $wpdb->prepare( 
                     " AND {$wpdb->posts}.post_date <= %s",
                      $dq['before']
                   );
    }

    return $where;
}
add_filter( 'post_where', 'my_pseudo_date_query', 10, 2 );

Example usage:

$now = new DateTime( 'now' );
$first_of_last_month = new DateTime( 'first day of last month' );
$my_query = new WP_Query( array(
 'my_date_query' => array(
      'before' => $now->format( 'Y-m-d H:i:s' ),
      'after' => $first_of_last_month->format( 'Y-m-d H:i:s' ),
  );
 ) );

The before argument is redundant in this example.

Remarks

  1. This hasn't been tested, it's code 'in principle'.
  2. I've been quite lazy here and the before and after keys of the my_date_query attribute are quite strict. They must be in the Y-m-d H:i:s (php) format. But it's enough for this answer.
  3. As noted in the comments WordPress 3.7 will provide date_query as an argument. The above code is 'forward compatible' in the sense that once you've upgraded to 3.7, you can remove the my_ prefix from the example.
  4. The example assumes PHP5.3+. Though its fairly simple to workaround this limitation.
  5. If using with get_posts() don't forget to set suppress_filters to false.
1
  • Thanks Stephen, as you point out, the issue with the filter was that get_posts defaults suppress_filters to true :) Thanks for the explanation, looking forward to have 3.7 on production sites ;)
    – versvs
    Commented Oct 1, 2013 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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