Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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