2021-04-27

Is it possible to match arrays of terms with array_intersect() in Wordpress?

Or must I use something like this with get_post() for the purpose:

$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',
        ),
    )
);

The goal is to match the whole array of terms for the next/prev posts, since get_next_post has an OR conditional, not an AND.

With the native WP get_next_post mechanism with in_same_term set to true that my current buttons use, if current post has terms A, B and C, it would fetch next post that has A and C only, or A only, or A and B only, it would would even fetch a post that has A, B, C and D. In contrast, what I need is:

If the current post has terms A, B, and C, the next/prev posts must also have all terms A, B and C, and no extras like D should be allowed, meaning: identic array of terms.

So A and C only, or A only, or A and B only or A B C D shouldn’t be allowed.



from Recent Questions - Stack Overflow https://ift.tt/3aIm9Or
https://ift.tt/eA8V8J

No comments:

Post a Comment