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.

(Moderator's note: Title was "Convert into nice URL a shared custom taxonomy URL with a query string value")

I have a custom taxonomy template: taxonomy-country.php used by two (2) Custom Post Types: 'event' and 'contact'. I would like to set up pages to display, for example:

  • "All Costa Rica Contacts", and

  • "All United States Events"

These URLs current give me those lists:

But I would like to implement those URLs as nice URLs like this:

Or even better, something like:

share|improve this question

2 Answers 2

For the first preference, you'd need to filter in additional rewrite rules like so;

function __extra_country_rewrite_rules( $rules )
{
    global $wp_rewrite;
    if ( !isset( $wp_rewrite ) )
        $wp_rewrite = new WP_Rewrite;

    $m1 = $wp_rewrite->preg_index(1); // preg match backreferences
    $m2 = $wp_rewrite->preg_index(2);
    $m3 = $wp_rewrite->preg_index(3);

    $rules['country/([^/]+)/([^/]+)s/feed/(feed|rdf|rss|rss2|atom)/?$'] = "index.php?country=$m1&post_type=$m2&feed=$m3";
    $rules['country/([^/]+)/([^/]+)s/(feed|rdf|rss|rss2|atom)/?$'] = "index.php?country=$m1&post_type=$m2&feed=$m3";
    $rules['country/([^/]+)/([^/]+)s/page/?([0-9]{1,})/?$'] = "index.php?country=$m1&post_type=$m2&paged=$m3";
    $rules['country/([^/]+)/([^/]+)s/?$'] = "index.php?country=$m1&post_type=$m2";

    return $rules;
}
add_filter( 'country_rewrite_rules', '__extra_country_rewrite_rules' );

The second one is less dynamic, as you'd need to hardcode the post types in your rewrites (there is no initial 'indentifier' in the URL, unlike 'country' in the former).

function __extra_country_rewrite_rules( $rules )
{
    global $wp_rewrite;
    if ( !isset( $wp_rewrite ) )
        $wp_rewrite = new WP_Rewrite;

    $m1 = $wp_rewrite->preg_index(1); // preg match backreferences
    $m2 = $wp_rewrite->preg_index(2);
    $m3 = $wp_rewrite->preg_index(3);

    $rules["(event|contact)s/country/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"] = "index.php?post_type=$m1&country=$m2&feed=$m3";
    $rules["(event|contact)s/country/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"] = "index.php?post_type=$m1&country=$m2&feed=$m3";
    $rules["(event|contact)s/country/([^/]+)/page/?([0-9]{1,})/?$"] = "index.php?post_type=$m1&country=$m2&paged=$m3";
    $rules["(event|contact)s/country/([^/]+)/?$"] = "index.php?post_type=$m1&country=$m2";

    return $rules;
}
add_filter( 'rewrite_rules_array', '__extra_country_rewrite_rules' );

You'll need to flush your permalinks for changes to take effect (just visit the Permalinks settings page once the code has been added).

If you find that things still aren't functioning correctly, update your question with the code you use to register your post types & custom taxonomies.

share|improve this answer

@TheDeadMedic I can't comment so i post it as an answer. Could you please explain for the second rewrite rules how do i have to register my custom post types and taxonomy regarding the rewrite slug? Do i have to set it to false or give it the same value as in the rewrite rules, in this example 'event' and 'contact' for the two cpt and 'country' for the taxonomy?

share|improve this answer

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.