2011-07-27 54 views
0

我正在尝试使用发布的片段here来显示自定义查询的页面。以下是我的functions.php如何获得在Wordpress中运行的自定义查询?

function alter_the_query($request) { 
    $dummy_query = new WP_Query(); 
    $dummy_query->parse_query($request); 
    // this is the actual manipulation 
    if($dummy_query->is_page('blog')) $request['category_name'] = 'Blog'; 
    // and then 
    return $request; 
} 
add_filter('request','alter_the_query'); 

称为“博客”的网页有一个基本的循环内 - 所以应该显示任何WP_Query告诉它。但是,它只显示页面内容 - 应该被忽略。

我似乎无法找到解释$request对象的属性的参考,所以我不知道该怎么做才能使此片段正常工作。有任何想法吗?

回答

0

我想这是因为你没有改变$ request ['pagename']的值,所以wp尝试加载这个页面的内容。

更新这应该为你

function alter_the_query($request) { 
    $dummy_query = new WP_Query(); 
    $dummy_query->parse_query($request); 
    // this is the actual manipulation 
    if($dummy_query->is_page('blog')) { 
     $request['category_name'] = 'Blog'; 
     unset($request['pagename']); 
    } 
    // and then 
    return $request; 
} 
+0

工作,你有'$ request'的性能基准? –

+0

请求与WP类中的属性public_query_vars具有相同的属性(wp-includes/class-wp.php第18行)。另请看private_query_vars属性。参数描述http://codex.wordpress.org/Class_Reference/WP_Query#Parameters – hadvig

+0

关于错误:这看起来像你没有从你的过滤器返回值或返回的值不是数组。 – hadvig