Very complex post query

时间:2021-03-17 作者:Peter K.

我需要创建一个非常复杂的post查询,但我不知道这样的查询是否存在,或者我是否必须编写PHP代码来组合两个不同查询的结果。。。

请让我先描述一下情况,然后写下我迄今为止所做的工作。

所以,我需要返回CPT的结果。CPT有自己的分类法,我在其中插入了必要的术语。这些术语与CPT本身的不同类型有关。有一个特定的术语,我不想从中获得结果,有大多数术语我想检索给定日期的结果,有3个特定的术语,我想要相同给定日期最后5天的结果。

如果是这样的话,我只需编写两个不同的查询,并将结果合并到一个数组中进行显示。

问题是,所有返回的帖子(给定日期的帖子和过去5天的帖子)都需要按自定义字段排序。在我的单个查询中,我使用适当的orderby参数考虑了排序,但当组合起来时,总排序就消失了,因为我得到的是第一个查询的结果,然后是第二个查询的结果,分别排序;我需要将它们组合在一起进行排序。所以让我来编写查询的代码。。。

这是第一次排除4个具体条款:

$args1 = array(
    \'post_type\' => \'frontpage\',
    \'orderby\' => \'meta_value_num\',
    \'order\' => \'ASC\',
    \'meta_key\' => \'frontpage_order\',
    \'posts_per_page\' => -1,
    \'date_query\' => array(
        array(
            \'year\' => date(\'Y\', $date),
            \'month\' => date(\'m\', $date),
            \'day\' => date(\'d\', $date),
        ),
    ),
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'newspaper_genre\',
            \'field\' => \'term_id\',
            \'terms\' => array(
                7670,
                7674,
                7684,
                7686,
            ),
            \'operator\' => \'NOT IN\',
        ),
    ),
);
第二个仅包括3个具体术语:

$args2 = array(
    \'post_type\' => \'frontpage\',
    \'orderby\' => \'meta_value_num\',
    \'order\' => \'ASC\',
    \'meta_key\' => \'frontpage_order\',
    \'posts_per_page\' => -1,
    \'date_query\' => array(
        array(
            \'after\' => array(
                \'year\' => date(\'Y\', $date_minus_five),
                \'month\' => date(\'m\', $date_minus_five),
                \'day\' => date(\'d\', $date_minus_five),
            ),
            \'before\' => array(
                \'year\' => date(\'Y\', $date),
                \'month\' => date(\'m\', $date),
                \'day\' => date(\'d\', $date),
            ),
            \'inclusive\' => true,
        ),
    ),
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'newspaper_genre\',
            \'field\' => \'term_id\',
            \'terms\' => array(
                7674,
                7684,
                7686,
            ),
            \'operator\' => \'IN\',
        ),
    ),
);
因此,问题是,我是否缺少一个查询设置,在该设置中,我可以以某种方式将特定的date\\u query参数与特定的tax\\u query参数相结合,并将其他date\\u query参数与其他tax\\u query参数以及所有这些参数组合在一个查询中?

还是必须合并两个检索到的结果集,然后以编程方式重新排序?

TIA公司

1 个回复
最合适的回答,由SO网友:Peter K. 整理而成

解决问题的灵感来自this 线

其想法是创建单独的查询(无序以节省时间,因为在这个阶段不需要排序),以获得符合条件的帖子ID;然后使用这些ID并以这些ID为标准运行最终查询,以获得完整的帖子(已排序以及此阶段的所有内容)。这样的代码如下所示:

$ids1 = get_posts(array(
    \'fields\' => \'ids\',
    \'post_type\' => \'frontpage\',
    \'posts_per_page\' => -1,
    \'date_query\' => array(
        array(
            \'year\' => date(\'Y\', $date),
            \'month\' => date(\'m\', $date),
            \'day\' => date(\'d\', $date),
        ),
    ),
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'newspaper_genre\',
            \'field\' => \'term_id\',
            \'terms\' => array(
                7670,
                7674,
                7682,
                7684,
                7686,
                8099,
                8101,
            ),
            \'operator\' => \'NOT IN\',
        ),
    ),
));

$ids2 = get_posts(array(
    \'fields\' => \'ids\',
    \'post_type\' => \'frontpage\',
    \'posts_per_page\' => -1,
    \'date_query\' => array(
        array(
            \'year\' => date(\'Y\', $date),
            \'month\' => date(\'m\', $date),
        ),
    ),
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'newspaper_genre\',
            \'field\' => \'term_id\',
            \'terms\' => array(
                7682,
                8099,
                8101,
            ),
            \'operator\' => \'IN\',
        ),
    ),
));

$ids3 = get_posts(array(
    \'fields\' => \'ids\',
    \'post_type\' => \'frontpage\',
    \'posts_per_page\' => -1,
    \'date_query\' => array(
        array(
            \'after\' => array(
                \'year\' => date(\'Y\', $date_minus_five),
                \'month\' => date(\'m\', $date_minus_five),
                \'day\' => date(\'d\', $date_minus_five),
            ),
            \'before\' => array(
                \'year\' => date(\'Y\', $date),
                \'month\' => date(\'m\', $date),
                \'day\' => date(\'d\', $date),
            ),
            \'inclusive\' => true,
        ),
    ),
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'newspaper_genre\',
            \'field\' => \'term_id\',
            \'terms\' => array(
                7674,
                7684,
                7686,
            ),
            \'operator\' => \'IN\',
        ),
    ),
));

$query = get_posts(array(
    \'include\' => array_merge($ids1, $ids2, $ids3),
    \'post_type\' => \'frontpage\',
    \'orderby\' => array(
        \'meta_value_num\' => \'ASC\',
        \'date\' => \'ASC\',
    ),
    \'meta_key\' => \'frontpage_order\',
));