2

(Update: Please see the edits far below.)

The code:

function itsme_better_editions( $query ) {
    if ( $query->is_category() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post' ) );
        $query->set( 'tax_query',
            array(
                'relation' => 'OR',
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => 'intl',
                    'operator' => 'IN'
                )
            )
        );
    }
    return $query;
}
add_filter( 'pre_get_posts', 'itsme_better_editions' );

I am using this function to display posts assigned to 'International (intl)' category under all categories i.e. in all category archives.

(In my case categories represent Editions as common with a news site, for example.)

The problem is, with the function in place, it appears as though the term object of all archives gets set to 'International (intl)', be it a category archive, tag archive, custom taxonomy archive, etc.

So, for example, if I visit the 'UK (uk)' category archive, the <?php single_cat_title(); ?> displays 'International' instead of 'UK'. (NOTE: single_cat_title() is just an example, and only one of the problems caused by the change in term object.)

Why is this happening? (Despite me setting 'relation' => 'OR'.) What should I do to fix this?

PS: Surprisingly this used to work (very positive), and I don't know in which version of WordPress this broke.

EDIT: The problem, I just realized, appears to be that $tax_query = $query->get( 'tax_query' ); is empty or not an array. I am not really sure why this is happening, and what's wrong with my code.

Related Question: When/why does '$query->get( 'tax_query' );' return empty?

  • var_dump( $wp_query ) in your template and see what the query and arguments look like. I'd guess that by doing this you've no longer got a straight category archive query. – Milo Nov 21 '13 at 18:16
  • 3
    I think the relation argument only applies to the arrays inside the tax_query array. WordPress does not "know" that you want to OR the tax_query with the rest of the outer results. – Charles Clarkson Nov 21 '13 at 18:20
  • @Milo Did that. Couldn't figure anything from it by myself: pastebin.com/PDTAJb14 – its_me Nov 21 '13 at 19:02
1

The issue is you are overriding the entire tax_query. When you go to the UK category archive, tax query is set to have the same thing you have for International, but you are nuking the current query. Here is the solution:

...
$tax_query = $query->get('tax_query');
$tax_query['relation'] = 'OR';
// puts the item at the beginning of the array instead of the end.
array_unshift( $tax_query, array(
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => 'intl',
        'operator' => 'IN'
    );
$query->set( 'tax_query', $tax_query );
...
  • I thought it'd work. But it didn't change anything. Here's a var_dump in case you can see what's wrong: pastebin.com/PDTAJb14. And here's the full function (modified as per your instructions), just in case: gist.github.com/anonymous/31518bbee8aa1f50a243 – its_me Nov 21 '13 at 20:30
  • Well I can see you are getting posts back. What's the issue - it's only Intl ones, no UK? – Eric Holmes Nov 21 '13 at 21:06
  • Posts that belong to the current category archive page's term OR the 'intl' category are being shown. But it appears that the category object for the page is set to point to 'intl' category and NOT the category archive page's term. For instance, <?php single_cat_title(); ?> shows "International" instead of "UK" for UK's archive page. – its_me Nov 22 '13 at 2:18
  • Perhaps try using array_unshift to at the Intl tax_query item on. See edit above. It might make UK be the 'single_cat_title'. – Eric Holmes Nov 22 '13 at 14:09
  • I get this error when using array_unshift(): "Warning: array_unshift() expects parameter 1 to be array, null given in /var/www/.../functions.php on line ...". As stated at the end of my question, $tax_query = $query->get( 'tax_query' ); is returning empty, not an array as it should. This is weird, but it happens even the default Twenty Thirteen theme. And also please take a look at the related question added as a link in the question, or here: wordpress.stackexchange.com/q/124100/10691 – its_me Nov 22 '13 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.