Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Wordpress中运行自定义查询?_Php_Wordpress - Fatal编程技术网

Php 如何在Wordpress中运行自定义查询?

Php 如何在Wordpress中运行自定义查询?,php,wordpress,Php,Wordpress,我正在尝试使用发布的代码片段获取一个页面来显示自定义查询。下面是我的functions.php中的内容: 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')) $

我正在尝试使用发布的代码片段获取一个页面来显示自定义查询。下面是我的
functions.php
中的内容:

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');
名为“Blog”的页面内部有一个基本循环,因此它应该显示
WP\u Query
告诉它的任何内容。但是,它只显示页面内容,这应该被忽略


我似乎找不到一个解释
$request
对象属性的引用,所以我不确定该怎么做才能使这个代码段正常工作。有什么想法吗?

我想这是因为您没有更改$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;
}

您是否有
$request
属性的参考?request与WP类中的属性public\u query\u vars具有相同的属性(WP includes/class-WP.php第18行)。还可以查看private_query_vars属性。此处的参数说明关于错误:看起来您没有从筛选器返回值,或者返回的值不是数组。是的,它是。但我不知道为什么。我用稍微不同的方法(改变
$query\u vars
)做了一些修改,但只在一个布局上显示了每一页。做我想做的事情不会这么难。支持论坛上有几十个人问同样的问题,但没有答案。很明显,我缺少了一些关于如何操作查询的内容。我在测试wordpress的安装中尝试了这段代码,效果很好。也许我误解了你的问题。为什么不为页面创建不同的模板并使用此模板中的查询帖子?