birgir / combined-query
修改 WP_Query 以支持 combined_query 参数。将多个 WP_Query 查询组合成一个查询。
Requires
- php: >=5.4
- composer/installers: ^1.0.0
README
WordPress 插件 - Combine Query
描述
这个实验性插件允许您使用 combined_query
属性将多个 WP_Query
查询组合成一个。
这个想法最初是在 Stackoverflow 上的一个回答中提出的,请参见这里和这里。
这个插件背后的想法是将每个 WP_Query()
查询的 SQL 查询通过 UNION
或 UNION ALL
组合在一起。
我第一次注意到这个技巧是在 Mike Schinkel 在 WordPress 开发 上的一个很好的回答中。
我使用了这里提到的技巧来保持 UNION
子查询的顺序。
这个实现支持组合 N
个子查询。
关于新 1.0.0 版本的说明
这个版本是插件的彻底重写。
已移除 WP_Combine_Query
类,改为直接使用 WP_Query
类的 combined_query
属性。
现在插件只支持 PHP 版本 5.4+。
设置
支持 combined_query
属性的设置
'combined_query' => [
'args' => [ $args1, $args2, ... ], // Array (default [])
'union' => 'UNION', // String Possible values are UNION or UNION ALL (default UNION)
'posts_per_page' => 10, // Integer 1,2,...
'offset' => 0, // Integer 0,1,...
'orderby' => 'meta_value_num', // String (post_name, ..., name, ..., none, meta_value, meta_value_num )
'order' => 'DESC', // String (ASC,DESC)
]
如果您想删除重复的帖子,请使用 UNION
,否则请使用 UNION ALL
。
自定义过滤器
目前有两个可用的自定义过滤器
// Modify combined ordering:
add_filter( 'cq_orderby', function( $orderby ) {
return $orderby;
});
// Modify sub fields:
add_filter( 'cq_sub_fields', function( $fields ) {
return $fields;
});
要按 arg1、arg2 等参数的顺序保持顺序,请使用
'combined_query' => [
...
'orderby' => 'none',
...
]
或
add_filter( 'cq_orderby', '__return_empty_string' );
$query = new WP_Query( $args );
remove_filter( 'cq_orderby', '__return_empty_string' );
安装
将插件上传到插件文件夹并激活它。
使用 Composer 安装依赖项(不是必需的)
composer install
或
php composer.phar install
在我们的文件夹内。有关如何安装 Composer 的更多信息,请参阅此处。
然后尝试以下示例,在您的主题或插件中。
祝您玩得开心 ;-)
示例 1
这里我们想按字母顺序显示第一个发布的页面,然后显示三个最旧的发布的帖子
//-----------------
// Sub query #1:
//-----------------
$args1 = [
'post_type' => 'page',
'posts_per_page' => 1,
'orderby' => 'title',
'order' => 'asc',
];
//-----------------
// Sub query #2:
//-----------------
$args2 = [
'post_type' => 'post',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'asc',
];
//---------------------------
// Combined queries #1 + #2:
//---------------------------
$args = [
'combined_query' => [
'args' => [ $args1, $args2 ],
'union' => 'UNION',
'posts_per_page' => 4,
'orderby' => 'none',
]
];
//---------
// Output:
//---------
$q = new WP_Query( $args );
if( $q->have_posts() ):
?><ul><?php
while( $q->have_posts() ): $q->the_post();
?><li><a href="<?php the_permalink();?>"><?php the_title();?></a></li><?php
endwhile;
?></ul><?php
wp_reset_postdata();
else:
_e( 'Sorry no posts found!' );
endif;
示例 2
这里我们想按评论数排序显示所有 foo 帖子和日期查询,然后按评论数排序显示所有 bar 帖子。然后按评论数降序排序。
//-----------------
// Sub query #1:
//-----------------
$args1 = [
'post_type' => 'foo',
'orderby' => 'comment_count',
'order' => 'desc',
'posts_per_page' => 100, // adjust to your needs
'date_query' => [
[
'after' => date('Y-m-d'),
],
'inclusive' => true,
]
];
//-----------------
// Sub query #2:
//-----------------
$args2 = [
'post_type' => 'bar',
'orderby' => 'comment_count',
'order' => 'desc',
'posts_per_page' => 100, // adjust to your needs
];
//---------------------------
// Combined queries #1 + #2:
//---------------------------
$args = [
'combined_query' => [
'args' => [ $args1, $args2 ],
'posts_per_page' => 5,
'paged' => 2,
'orderby' => 'comment_count',
'order' => 'desc',
]
];
//---------
// Output:
//---------
// See example 1
示例 3
让我们组合两个元查询并按公共元值排序
//-----------------
// Sub query #1:
//-----------------
$args1 = [
'post_type' => 'cars',
'posts_per_page' => 10,
'orderby' => 'title',
'order' => 'asc',
'meta_query' => [
[
'key' => 'doors',
'value' => 0,
'compare' => '>=',
'type' => 'UNSIGNED'
],
],
];
//-----------------
// Sub query #2:
//-----------------
$args2 = [
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'desc',
'tax_query' => [
[
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'cars',
],
],
'meta_query' => [
[
'key' => 'doors',
'value' => 0,
'compare' => '>=',
'type' => 'UNSIGNED'
],
],
];
//------------------------------
// Order by a common meta value
//------------------------------
// Modify sub fields:
add_filter( 'cq_sub_fields', $callback = function( $fields ) {
return $fields . ', meta_value';
});
//---------------------------
// Combined queries #1 + #2:
//---------------------------
$args = [
'combined_query' => [
'args' => [ $args1, $args2 ],
'posts_per_page' => 5,
'orderby' => 'meta_value_num',
'order' => 'DESC',
]
];
//---------
// Output:
//---------
// See example 1
remove_filter( 'cq_sub_fields', $callback );
示例 4
我们也可以组合超过两个子查询,以下是一个四个子查询的示例
$args = [
'combined_query' => [
'args' => [ $args1, $args2, $args3, $args4 ],
...
]
];
//---------
// Output:
//---------
// See example 1
示例 5
上面的示例都是针对次要查询的。所以让我们向主首页查询添加一个查询
add_action( 'pre_get_posts', function( \WP_Query $q ) {
if( $q->is_home() && $q->is_main_query() ) {
//-----------------
// Sub query #1:
//-----------------
$args1 = [
'post_type' => 'page',
'posts_per_page' => 1,
'orderby' => 'title',
'order' => 'asc',
];
//-----------------
// Original query #2:
//-----------------
$args2 = $q->query;
//---------------------------
// Combined queries #1 + #2:
//---------------------------
$args = [
'combined_query' => [
'args' => [ $args1, $args2 ],
'union' => 'UNION',
'posts_per_page' => 4,
'orderby' => 'none',
]
];
//-----------------------
// Modify the Main query:
//-----------------------
$q->set( 'combined_query', $args['combined_query'] );
}
} );
变更日志
(2022-03-03)
- 修复:头部中错误的插件 URI(感谢:therealgilles)
1.2.2 (2021-02-20)
- 修复:修复了 #14 有关后续查询的问题。(感谢:@Suranex)
1.2.1 (2020-09-14)
- 修复:Readme。
1.2.0 (2020-09-14)
- 添加:支持按 'none' 排序。
- 添加:测试用例。
- 添加:支持在 combined_query 参数中添加参数(posts_per_page、offset、orderby、order)。
- 修复:pre_get_posts 示例中的钩子处理。
- 调整:示例。
1.1.1 (2020-09-04)
- 添加:如何按 arg1、arg2 等参数的顺序保持顺序的示例。
- 调整:UNION ALL 测试
1.1.0 (2020-09-04)
- 添加:问题 #19 - 为参数顺序绕过添加测试用例。(感谢:therealgilles)
- 清理:phpcs
1.0.5 (2016-05-08)
- 修复:针对不使用Composer的用户提供回退方案。
- 改进:通过使用关键字移除了对$GLOBAL['wpdb']的显式调用。
- 改进:将命名空间简化为CombinedQuery。
1.0.4 (2016-04-21)
- 修复:调整了版本1.0.2中悄悄引入的分页错误。
- 改进:简化了使用get_query_var()的示例,现在可以处理默认输入参数。
1.0.3 (2016-04-21)
- 修复:工单#7 - 无法设置"UNION ALL"联合选项
- 改进:内联文档
1.0.2 (2016-04-20)
- 修复:工单#6 - 在Generator类中转义%。感谢:@DArcMattr
- 改进:内联文档
1.0.1 (2015-11-09)
- 修复:移除供应商依赖,并允许用户通过'composer install'安装(感谢:@pdufour)
- 修复:在EmptyQuery类中忽略粘性帖子
1.0.0 (2015-05-10)
- ** 总插件重写 **
- 关闭:工单#3
- 新增:新的类Main、EmptyQuery和Generator。
- 新增:支持WP_Query类的'combined_query'属性。
- 新增:仅支持PHP 5.4+
- 新增:通过Composer自动加载。
- 新增:新的过滤器'cq_sub_fields'代替'cq_sub_fields'
- 新增:新的过滤器'cq_orderby'代替'cq_orderby'
0.1.3 (2015-05-09)
- 新增:支持ignory_sticky_posts。
- 修复:小错误
0.1.2 (2015-05-08)
- 新增:支持GitHub Updater。
- 新增:新的过滤器'wcq_sub_fields'
- 新增:新的过滤器'wcq_orderby'
- 新增:新的示例用于元值排序
- 修复:排序不正确。
0.1.1
- 更改:编码风格和自动加载(感谢:@egill)
0.1各种插件改进,例如
- 新增:在组合查询中添加orderby。
- 新增:在子查询中添加posts_per_page。
- 新增:在子查询中添加offset。
- 新增:在子查询中添加paged。
- 移除:在组合查询中移除sublimit,在子查询中使用posts_per_page代替。
- 修复:与max_num_pages相关的问题#1(感谢:@hellofantastic)。
0.0.4
- 新增:在组合查询中添加对offset的支持。
0.0.3
- 新增:GPL2+许可部分
- 移除:删除命名空间和匿名函数以支持更广泛的PHP。
0.0.2
- 新增:输入参数'union',可能的值为UNION和UNION ALL。
- 修复:空的分页导致SQL错误。(感谢:Robert Hue)