首页 > 解决方案 > 如何在 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()但输出不是我所期望的。有什么想法可以解决吗?

先感谢您。

标签: phpwordpresswordpress-theming

解决方案


您可以有多个带有数组的 post_types,如下所示:

$query = new WP_Query( array(
        'post_type' => array('reply', 'topic')
    ) );

foreach ($query->posts as $p){     
    echo 'ID :'.$p->ID."<br />\r\n";
}

要查看更多详细信息,请查看此链接


推荐阅读