Wordpress查询帖子标题为>另一个标题

时间:2015-05-04 08:55:02

标签: wordpress wp-query

我正在尝试查询自定义帖子类型,该类型会在特定标题后按字母顺序搜索下3个帖子。 wp_query()的参数片段是

$vars = array('paged' => $page, 'posts_per_page' => intval($posts), 
  'order' => 'asc', 'offset' => ($offset ? $offset : ''), 'orderby' => 'title' );

但不知道如何查询标题>的帖子具体标题。

1 个答案:

答案 0 :(得分:1)

您可以使用posts_where过滤器修改WP_Query

的where子句
//create function to handle where clause change
function add_title_clause($where) {
    $titleParam = 'C'; //change it according to your needs

    return $where . " AND post_title > '" . $titleParam . "'";

}


//attach your function to the posts_where filter
add_filter( 'posts_where' , 'add_title_clause' );

$vars = array('paged' => $page, 'posts_per_page' => intval($posts), 
  'order' => 'asc', 'offset' => ($offset ? $offset : ''), 'orderby' => 'title' );

$results = new WP_Query($vars);

//remove filter so it does not affect other queries in same request
remove_filter( 'posts_where' , 'add_title_clause' );