0

Question updated.

What I'm trying to do

If I want to display posts (or a feed of posts) that belong to a category AND a custom taxonomy, or a category AND a tag, or a custom taxonomy AND a tag, I can do so using query variables as query strings in URL.

But I want to prettify the URLs first, like so:

  • example.com/?category_name=main&channel=tech to example.com/main/channel/tech/

  • example.com/?category_name=main&channel=tech&feed=rss2 to example.com/main/channel/tech/feed/

  • example.com/?category_name=main&tag=jsyk to example.com/main/tag/jsyk/

  • example.com/?category_name=main&tag=jsyk&feed=rss2 to example.com/main/tag/jsyk/feed/

Where category_name defines the category slug, channel the custom taxonomy "Channel" term's slug, and tag the tag slug.

How I'm doing it

First, I tried reading as many examples as possible, including:

I place all theme independent functions for my site in a must-use plugin, which looks like this:

<?php
/*
 * Plugin Name: Default Functions
 * Description: Site-specific functions for example.com
 */

/* Add functions below */



/* THE END */

That's where I added my rewrite function which looks like this:

function wtnerd_edition_specific_categories( $wp_rewrite ) {
    $rewrite_rules = array(
        '([a-zA-Z0-9-_^/]+)/channel/([a-zA-Z0-9-_^/]+)' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&channel='.$wp_rewrite->preg_index(2),

        '([a-zA-Z0-9-_^/]+)/channel/([a-zA-Z0-9-_^/]+)/feed' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&channel='.$wp_rewrite->preg_index(2).'&feed=rss2',

        '([a-zA-Z0-9-_^/]+)/section/([a-zA-Z0-9-_^/]+)' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&tag='.$wp_rewrite->preg_index(2),

        '([a-zA-Z0-9-_^/]+)/section/([a-zA-Z0-9-_^/]+)/feed' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&tag='.$wp_rewrite->preg_index(2).'&feed=rss2'
    );
    $wp_rewrite->rules = $rewrite_rules + $wp_rewrite->rules;
}
add_filter( 'generate_rewrite_rules', 'wtnerd_edition_specific_categories' );

Problem

The feed URLs don't work. For example:

example.com/main/channel/tech/feed/

loads the content-none.php template instead of showing the actual feed of posts that belong to the said terms.

I am not sure how to fix this.

1

You can use add_rewrite_rule hooked to the init action, instead of using the generate_rewrite_rules filter (where it gets a bit low-level).

But the actual problem with your rewrite rules is the regex in place. Here's what it'd look like:

function wtnerd_edition_specific_categories() {
    add_rewrite_rule( '(.+?)/channel/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?category_name=$matches[1]&channel=$matches[2]&feed=$matches[3]', 'top' );
    add_rewrite_rule( '(.+?)/channel/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?category_name=$matches[1]&channel=$matches[2]&feed=$matches[3]', 'top' );
    add_rewrite_rule( '(.+?)/channel/(.+?)/page/?([0-9]{1,})/?$', 'index.php?category_name=$matches[1]&channel=$matches[2]&paged=$matches[3]', 'top' );
    add_rewrite_rule( '(.+?)/channel/(.+?)/?$', 'index.php?category_name=$matches[1]&channel=$matches[2]', 'top' );

    add_rewrite_rule( '(.+?)/section/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?category_name=$matches[1]&tag=$matches[2]&feed=$matches[3]', 'top' );
    add_rewrite_rule( '(.+?)/section/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?category_name=$matches[1]&tag=$matches[2]&feed=$matches[3]', 'top' );
    add_rewrite_rule( '(.+?)/section/(.+?)/page/?([0-9]{1,})/?$', 'index.php?category_name=$matches[1]&tag=$matches[2]&paged=$matches[3]', 'top' );
    add_rewrite_rule( '(.+?)/section/(.+?)/?$', 'index.php?category_name=$matches[1]&tag=$matches[2]', 'top' );
}
add_action( 'init', 'wtnerd_edition_specific_categories' );

You can also use rewrite_rules_array filter hook.

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.