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 use this function to exclude from the query posts that are 30 days old,

function filter_where($where = '') {
  //posts in the last 30 days
  $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
  return $where;
// Register the filtering function
add_filter('posts_where', 'filter_where');
// Perform the query, the filter will be applied automatically

is it possible to do something like this to exclude posts that have a meta_key=votes and meta_value=0 ?

share|improve this question
up vote 1 down vote accepted

I think you'll need to add a posts_join filter, to join the posts and postmeta tables.

This link should be helpful: http://codex.wordpress.org/Custom_Queries, in particular this piece of example code:

add_filter('posts_join', 'geotag_search_join' );
add_filter('posts_where', 'geotag_search_where' );

function geotag_search_join( $join )
  global $geotag_table, $wpdb;

  if( is_search() ) {
    $join .= " LEFT JOIN $geotag_table ON " . 
       $wpdb->posts . ".ID = " . $geotag_table . 
       ".geotag_post_id ";

  return $join;

function geotag_search_where( $where )
  if( is_search() ) {
    $where = preg_replace(
       "(post_title LIKE $1) OR (geotag_city LIKE $1) OR (geotag_state LIKE $1) OR (geotag_country LIKE $1)", $where );

  return $where;
share|improve this answer
Probably the right approach for now. And in this case, your WHERE clause would be testing for IS NULL or <> "0". – goldenapples Jan 21 '11 at 17:37

Or, in WP3.1:

$query['meta_query'] = array(
    'key' => 'votes',
    'value' => '0',
    'compare' => '!='
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.