In general circumstances, using URL parameters, you can list posts that belong to a specific category AND a custom taxonomy, like this:
http://example.com/category/cars/?edition=usa
Where, category
is the Category base you are using for categories on your site (WordPress Dashboard > Settings > Permalinks > Category base); edition
is the custom taxonomy’s base/slug; and usa
is a term under the custom taxonomy.
If you want to include more than one category/custom taxonomy, these might help:
http://example.com/category/cars/?edition=usa,india
http://example.com/?category_name=cars,books&edition=usa,india
And Feeds:
http://example.com/category/cars/?edition=usa,india&feed=rss2
http://example.com/?category_name=cars,books&edition=usa,india&feed=rss2
Further Reading:
BUT…
As explained in my question, mine is a complex case, so I’ve developed a simple work-around. Here goes…
-
The code block in the question makes sure that if a post is unassigned to any Edition (i.e. if a post is not assigned to any term that belongs to the custom taxonomy ‘edition’), the post is shown/listed under all term archives/feeds of the custom taxonomy ‘edition’.
BUT NOW, I removed that code. Then created a new term under the custom taxonomy ‘edition’, called ‘intl’ (International). Any post that want to be displayed under all Editions will be assigned to ‘intl’. But how do I make sure that all posts assigned to ‘intl’ show up in all term archives/feeds of my custom taxonomy?
For that, I now use this code (goes in functions.php):
add_filter('pre_get_posts','better_editions_archive'); function better_editions_archive( $query ) { if ( $query->is_tax( 'edition' ) && $query->is_main_query() ) { $query->set( 'post_type', array( 'post' ) ); $query->set( 'tax_query', array( 'relation' => 'OR', array( 'taxonomy' => 'edition', 'field' => 'slug', 'terms' => 'intl', 'operator' => 'IN' ) ) ); } return $query; }
So now, for instance,
http://example.com/edition/usa/
lists posts that belong to either ‘usa’ or ‘intl’ (which are terms under my custom taxonomy ‘edition’). Its feed,http://example.com/edition/usa/feed/
does the same as well. -
Back to the main problem of the question. How do I list posts that belong to a specific category AND an edition, using URL parameters?
For example, how do I list posts that belong to category ‘cars’ AND edition ‘usa’?
This is what the URL should look like:
http://example.com/category/cars/?edition=usa,intl
(since we also want those posts that are shown under all editions i.e. those posts assigned to term ‘intl’). As for feeds:http://example.com/category/cars/feed/?edition=india,intl
That’s it!
(Special thanks to @kaiser for his help.)
Notes
If you strictly want to modify the main query/loop right within the template, e.g. taxonomy-edition.php (in my case), here’s an example as to how it can be done:
<?php
$edition_term = get_term( get_queried_object(), 'edition' )->slug;
$better_editions = new WP_Query(
array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'edition',
'field' => 'slug',
'terms' => array( $edition_term, 'intl' )
)
)
)
);
?>
<?php /* Blah, Blah, Blah! */ ?>
<?php if ( $better_editions->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $better_editions->have_posts() ) : $better_editions->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<?php /* Blah, Blah, Blah! */ ?>
<?php endif; ?>
Nonetheless, unless you really, really have to, go with pre_get_posts
.