php - 如何在 WP_Query 中合并 2 个数据数组?- WordPress
问题描述
我想组合来自 WP_Query 的 2 个数组。
调用1个数据,代码如下:
$query = new WP_Query(array('post_type' => 'reply') );
foreach ($query->posts as $p){
echo 'ID :'.$p->ID."<br />\r\n";
}
如果使用变量,只需将其插入 WP_Query 参数。像这样:
$args1 = array('post_type' => 'reply');
$query = new WP_Query($args1);
foreach ($query->posts as $p){
echo 'ID :'.$p->ID."<br />\r\n";
}
但我的问题是,我想将它与'post_type' => 'topic'
例如 args1 的输出
'post_type' => 'reply':
ID :519
ID :517
ID :516
ID :515
ID :514
ID :500
ID :499
ID :485
ID :479
ID :478
和 args2 用于:
'post_type' => 'topic':
ID :498
ID :484
ID :483
ID :474
我想将它们结合起来。我尝试过array_merge()
,array_combine()
但输出不是我所期望的。有什么想法可以解决吗?
先感谢您。
解决方案
您可以有多个带有数组的 post_types,如下所示:
$query = new WP_Query( array(
'post_type' => array('reply', 'topic')
) );
foreach ($query->posts as $p){
echo 'ID :'.$p->ID."<br />\r\n";
}
要查看更多详细信息,请查看此链接。
推荐阅读
- presto - Presto 本地文件连接器测试
- javascript - 如何让登录用户在 Firestore 中创建文档(SDK Web 版本 9 模块化)
- python - 网页抓取时遇到问题不返回任何内容
- java - 如何在 Java Servlet 过滤器中拦截基本路径
- c# - 无法检索通过 C# 执行的 WSFC FailoverCluster Powershell 模块的结果
- oracle - Oracle ADF:页面片段未显示在区域内使用任务流
- flutter - Flutter:如何禁用高架按钮“onPressed:”并使其保持禁用状态
- node.js - 在 node.js 中对 Authorize.net 的异步调用未按预期工作
- fastboot - 闪烁后如何在android上启动twrp恢复(启动失败)?
- python - 如何使用python找到两个矩阵之间的差异并以0.005的绝对截止值限制迭代?