WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to sort the posts using GD Star Rating in the last 24 hours. I have two separate queries, but I don't know how to merge them.

          function filter_where($where = '') {
            //posts in the last 24 Hours
            $where .= "AND post_date > '" . date('Y-m-d H:i:s', strtotime('-24 hours')) . "'";
            return $where;
        add_filter('posts_where', 'filter_where');



I have no idea how to sort the posts from the last 24 hours using GD Star, but these two queries work perfectly fine individually. Thanks!

share|improve this question

The posts_where filters the 'WHERE' part of the SQL statement, and not the query string. So

$where .= "&post_date > '".date('Y-m-d H:i:s', strtotime('-24 hours'))."'";

should be

$where .= " AND post_date > '".date('Y-m-d H:i:s', strtotime('-24 hours'))."'";

Also as this hook is fired for every query (admin and public side), you should use some logic to only append the custom where statement when required.

Alternatively, you can add the filter just before your query and remove it again afterwards:

add_filter( 'posts_where', 'filter_where' );
$query_string ='gdsr_sort=thumbs&posts_per_page=10';
query_posts( $query_string );
remove_filter( 'posts_where', 'filter_where' );

See related Codex page.

share|improve this answer

Untested as I don't have the plugin installed. But I'd say this would be a better way to do it rather than using query_posts()

global $wp_query;
// Get the existing query and add in extra query args
$query = array_merge( $wp_query->query, array( 'gdsr_sort' => 'thumbs', 'posts_per_page' => 10 ) );
// Apply our filter
add_filter( 'posts_where', 'last_24_hours_filter_where' );
// Do our new query
$wp_query = new WP_Query($query);
// Remove the filter so not to effect anything else
remove_filter( 'posts_where', 'last_24_hours_filter_where' );

function last_24_hours_filter_where($where = '') {
    // Alter query to only return posts in the last 24 hours
    return $where . " AND post_date > '" . date( 'Y-m-d H:i:s' , strtotime( '-24 hours' ) ) . "'";
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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