从具有META_KEY和META_VALUE的查询帖子中排除

时间:2011-01-21 作者:Philip

我使用此功能从30天前的查询帖子中排除,

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
query_posts($query_string);
是否可以这样做来排除meta\\u键=投票且meta\\u值=0的帖子?

2 个回复
最合适的回答,由SO网友:anu 整理而成

我认为您需要添加一个posts\\u连接过滤器,以连接posts和postmeta表。

此链接应有帮助:http://codex.wordpress.org/Custom_Queries, 特别是这段示例代码:

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(
       "/\\(\\s*post_title\\s+LIKE\\s*(\\\'[^\\\']+\\\')\\s*\\)/",
       "(post_title LIKE $1) OR (geotag_city LIKE $1) OR (geotag_state LIKE $1) OR (geotag_country LIKE $1)", $where );
   }

  return $where;
}

SO网友:goldenapples

Or, in WP3.1:

$query[\'meta_query\'] = array(
    \'key\' => \'votes\',
    \'value\' => \'0\',
    \'compare\' => \'!=\'
    ),
结束

相关推荐

获取在Functions.php中设置的变量,并在我的Custom Post模板中回显它们

在我的函数中设置了以下函数。php文件,以允许我的自定义帖子类型“Slideshow”工作。add_action( \'the_post\', \'paginate_slide\' ); function paginate_slide( $post ) { global $pages, $multipage, $numpages; if( is_single() && get_post_type() == \'lom_s