当前位置:首页 > 网络科技 > WordPress 自定义 WP_Query 循环如何支持 tax_query or meta_query 参数

WordPress 自定义 WP_Query 循环如何支持 tax_query or meta_query 参数

2年前 (2024-08-28)admin网络科技183

WordPress 的循环函数都是 and 方式链接,对于单个的 meta_query 或者 tax_query 字段内部才能使用 or,那么如果我们想要实现 tax_query or meta_query 的关系,WordPress 默认是不提供支持的。

子凡最近为了满足泪雪网的功能需求,查找了大量的教程,甚至想过用两个 WP_Query 循环来实现这样的功能,后来通过 Google 搜索到一个相关的答案,为此子凡整理并分享出来,这样当你在使用 WordPress 的 WP_Query 来自定义循环,并且完美的支持 tax_query or meta_query。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * Modify WP_Query to support 'meta_or_tax' argument
 * to use OR between meta- and taxonomy query parts.
 * use '_meta_or_tax' => true in wp_query array
 */
add_filter( 'posts_where', function( $where, \WP_Query $q ){
	$tax_args = isset( $q->query_vars['tax_query'] ) ? $q->query_vars['tax_query'] : null;
	$meta_args = isset( $q->query_vars['meta_query'] ) ? $q->query_vars['meta_query'] : null;
	$meta_or_tax = isset( $q->query_vars['_meta_or_tax'] ) ? wp_validate_boolean( $q->query_vars['_meta_or_tax'] ) : false;
	// Construct the "tax OR meta" query
	if( $meta_or_tax && is_array( $tax_args ) &&  is_array( $meta_args )  )   {
		global $wpdb;
		$field = 'ID';// Primary id column
		$sql_tax = get_tax_sql(  $tax_args,  $wpdb->posts, $field );// Tax query
		$sql_meta = get_meta_sql( $meta_args, 'post', $wpdb->posts, $field );// Meta query
		// Modify the 'where' part
		if( isset( $sql_meta['where'] ) && isset( $sql_tax['where'] ) ) {
			$where = str_replace( [ $sql_meta['where'], $sql_tax['where'] ], '', $where );
			$where .= sprintf( ' AND ( %s OR  %s ) ', substr( trim( $sql_meta['where'] ), 4 ), substr( trim( $sql_tax['where']  ), 4 ) );
		}
	}
	return $where;
}, PHP_INT_MAX, 2 );

使用方式则是在 WP_Query 的参数数组里面添加一条’_meta_or_tax’ => true,简单的举个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$args = array(
	'post_status'			=> 'publish',
	'order'					=> 'ASC',
	'nopaging'				=> true,
	'_meta_or_tax'			=> true, //用于支持 tax_query OR meta_query 查询
	'tax_query'				=> array(
		'relation'		=> 'OR',
		array(
			'taxonomy'	=> 'category',
			'field'		=> 'id',
			'terms'		=> array(1,2,3),
			'operator'	=> 'IN'
		), 
	),
	'meta_query'			=> array(
		array(
			'key'		=> 'Headline',
			'value'		=> ''
			'compare'	=> '!='
		),
	)
);
$query = new WP_Query( $args );

这可能是一个非常小众的需求,但是用起来还是非常的爽,而且对于有相关功能需求的来说,以及用两次循环甚至三次循环来搞定简直不要不要的,子凡我要的就是最极简最极致的功能,也要最简单高效的代码。

更多关于WordPress优化及疑问可以添加留言

w.haolusi.com

本文链接:https://w.haolusi.com/wordpress-wp_query-tax_query-or-meta_query.html

扫描二维码推送至手机访问。

版权声明:本文由豪鲁斯兴趣网发布,如需转载请注明出处。

本文链接:https://w.haolusi.com/?id=1947

标签: WordPress优化
分享给朋友:

“WordPress 自定义 WP_Query 循环如何支持 tax_query or meta_query 参数” 的相关文章

In terms of equipment acquisition, what improvements can be made?

In terms of equipment acquisition, what improvements can be made?

In terms of equipment acquisition in World of Warcraft, Players often discuss some areas that can be improved to enhance the gaming experien...

Machine Learning: Transforming Data into Intelligence

Machine Learning: Transforming Data into Intelligence

Machine Learning (ML) is a transformative technology that leverages algorithms and statistical models to enable computers to learn from and make...

WordPress如何移除登录界面中英文语言切换框

WordPress如何移除登录界面中英文语言切换框

其实早在 WordPress 5.9 版本开始,WordPress 默认登录界面就增加了语言切换选项, 其目的就是允许用户自己选择要使用的语言登录后台。这个功能对于多语言网站或是国际性站点可能还有点用,但是对于国内用户来说就显得有些多余。最近子凡在做更深度的 WordPress 优化,同时也为了丰富...

最新免费可用!ChatGPT 4.0/4o/3.5 镜像 Mirror|免翻直链中文镜像(2024年8月更新)

最新免费可用!ChatGPT 4.0/4o/3.5 镜像 Mirror|免翻直链中文镜像(2024年8月更新)

本篇文章目录|Table of Contents Hide ChatGPT 4.0/4o/3.5 镜像站列表ChatGPT镜像更新历史什么是Cha...

Timeline Migrate Tools Boxset 剪辑时间线字幕迁移工具套件|FCPX vs PR|XML vs FCPXML|SRT vs XML|macOS|测试教程 Tutorial

Timeline Migrate Tools Boxset 剪辑时间线字幕迁移工具套件|FCPX vs PR|XML vs FCPXML|SRT vs XML|macOS|测试教程 Tutorial

本篇文章目录|Table of Contents Hide Final Cut Pro X 提取音频给音频编辑X2Pro Audi...

FCPX/PR的剪辑时间线XML与字幕SRT互转|字幕Srt转XML互转工具|xml转srt|srt转fcpxml|Tutorial

FCPX/PR的剪辑时间线XML与字幕SRT互转|字幕Srt转XML互转工具|xml转srt|srt转fcpxml|Tutorial

本篇文章目录|Table of Contents Hide 概述应用场景FCPX/PR剪辑时间线XML与SRT字幕互转获取与安装:直接安装 ...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。