Skip to main content
Bounty Ended with 100 reputation awarded by Janith Chinthana
Understood the problem better and offered an alternative solution.
Source Link
Vlad Olaru
  • 747
  • 4
  • 8

Or ifI understand that you have a multi-step logic to select the internalposts to display in certain categories. I have given it some thought and it would be quite cumbersome to implement this logic preventsin a tax query through various AND/OR relationships. I think you from doing this, one-level deepshould do all of the category selection in PHP and end up with OR relation:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 108, 109 ),
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 112, 113 ),
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 114, 115, 116 ),
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 117, 118 ),
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 102, 103 ),
            'operator' => 'IN',
        ),
    ),
);

Let me know how this goesan array of category ids that you would use like above. This will allow you to separate your logic from the actual posts selection.

Or if the internal logic prevents you from doing this, one-level deep with OR relation:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 108, 109 ),
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 112, 113 ),
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 114, 115, 116 ),
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 117, 118 ),
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 102, 103 ),
            'operator' => 'IN',
        ),
    ),
);

Let me know how this goes.

I understand that you have a multi-step logic to select the posts to display in certain categories. I have given it some thought and it would be quite cumbersome to implement this logic in a tax query through various AND/OR relationships. I think you should do all of the category selection in PHP and end up with an array of category ids that you would use like above. This will allow you to separate your logic from the actual posts selection.

Source Link
Vlad Olaru
  • 747
  • 4
  • 8

The query you need to use is not so complicated as you basically just what to get posts from only certain taxonomies. You are right that you should use the tax_query.

The first thing that you should do is collate all your category IDs in only one array as there is no need for multiple ones since you are only using the IN condition (that means posts that belong to at least one category in the list). It makes code error-prone and slow to have multiple tax queries when one would suffice.

The problem with your query is that you are using a AND relation that is never true in your case (ie. you don't have a post that is at the same time in categories 108 or 109, and in categories 112 or 113).

So I would suggest you use WP_Query args like so:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 108, 109, 112, 113, 114, 115, 116, 117, 118, 102, 103 ),
            'operator' => 'IN',
        ),
    ),
);

Or if the internal logic prevents you from doing this, one-level deep with OR relation:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 108, 109 ),
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 112, 113 ),
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 114, 115, 116 ),
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 117, 118 ),
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 102, 103 ),
            'operator' => 'IN',
        ),
    ),
);

Let me know how this goes.