Allow me to explain with an example, for the sake of clarity. Please take the time to read it all to get a good idea of what I am exactly after.
On my WordPress site categories are used as Editions and there are 4 of them — Category (slug):
- US (main)
- UK (uk)
- India (in)
- International (intl)
The permalink structure is /%category%/%year%/%monthnum%/%day%/%postname%/
. With this permalink structure in place, a post assigned to more than 1 category will be accessible via any of the category slugs in the permalink.
For example, if a post is assigned to US and UK categories, it\'ll be accessible via both of these URLs (no redirection):
http://example.com/main/2013/11/01/sample-post/
http://example.com/uk/2013/11/01/sample-post/
Points of note:
The permalink will still only include the slug of the category with the lowest ID.
If you try using the slug of a category that the post is not assigned to, it\'ll redirect back to the permalink. For instance, these two (note intl
and in
):
http://example.com/intl/2013/11/01/sample-post/
http://example.com/in/2013/11/01/sample-post/
Will redirect back to the permalink, i.e.
http://example.com/main/2013/11/01/sample-post/
The problem: For those posts that I want to be shown in all Editions (categories in my case), I can check all categories when publishing the post. But that\'d cause problems later on when I add more editions, i.e. the old posts won\'t be shown under the new Editions/categories.
So I use this custom function to suit my needs:
add_filter(\'pre_get_posts\',\'better_editions_archive\');
function better_editions_archive( $query ) {
if ( $query->is_tax( \'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;
}
/*
* Based on:
* http://wordpress.stackexchange.com/a/90573/10691
*/
With that function in place, when I want a post to be displayed in all Editions (categories), I can simply assign the post to International (intl) category and voila!
The problem is, now the post is only accessible via its permalink i.e. http://example.com/intl/2013/11/01/sample-post/
.
But I want it to be accessible (with no redirection) via all the category slugs, i.e.
http://example.com/main/2013/11/01/sample-post/
http://example.com/uk/2013/11/01/sample-post/
http://example.com/in/2013/11/01/sample-post/
Reason: In each Edition (category) archive, I want all the posts\' links to start with that specific category\'s slug i.e. if the archive URL is http://example.com/uk/
, I made sure that all post URLs\'d look like this:
http://example.com/uk/2013/11/01/sample-post-1/
http://example.com/uk/2013/11/01/sample-post-2/
http://example.com/uk/2013/11/01/sample-post-3/
I used str_replace()
for this, just so it\'s clear.
Now, the question is, when a post is assigned to International (intl) category, how can I make sure that it\'s accessible, without any redirection whatsoever, via all category slugs, as if the post were assigned to all the categories?
Precisely, how do I make assigning a post to a specific category equivalent to assigning it to all categories?