首页 > 解决方案 > 是否可以在 Wordpress 中将术语数组与 array_intersect() 匹配?

问题描述

或者我必须使用 get_post() 这样的东西来达到目的:

$args= array(
    'post_type' => 'post',
    'posts_per_page' => '1',
    'orderby' => 'rand',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => 'category-1',
            ),
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => 'category-2',
            ),
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => 'category-3',
            ),
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => 'category-4',
            ),
        ),
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => 'category-5',
        ),
    )
);

目标是匹配下一个/上一个帖子的整个术语数组,因为 get_next_post 具有 OR 条件,而不是 AND。

使用我当前按钮使用的 in_same_term 设置为 true 的本机 WP get_next_post 机制,如果当前帖子具有术语 A、B 和 C,它将获取仅具有 A 和 C、或仅 A、或仅 A 和 B 的下一个帖子,它甚至会获取包含 A、B、C 和 D 的帖子。相比之下,我需要的是:

如果当前帖子有术语 A、B 和 C,则下一个/上一个帖子也必须包含所有术语 A、B 和 C,并且不应允许像 D 这样的额外内容,意思是:相同的术语数组。

因此,不应允许仅 A 和 C,或仅 A,或仅 A 和 B,或 ABCD。

标签: phpmysqlarrayswordpress

解决方案


推荐阅读