0

I created a custom taxonomy, two actually (one tag and one category), for a page that lists a series of community projects. This is the parent URL:

website.com/community

If you go here you'll see ALL the projects. I've successfully set up query vars so that if you enter say:

website.com/community/?stencil-tag=easter 

or

website.com/community/?stencil-type=build

everything works as it should. It's perfect. Now, naturally, I'm trying to set up pretty permalinks so that users would simply be able to enter

website.com/community/tag/easter

and

website.com/commumity/type/build

respectively. I feel as if the issue may have something to do with the fact that I'm trying to rewrite on a child page of the root (/community)

Anyhow, this is my rewrite function. I've tried a bunch of deviations but I can't see to hit the mark here. I'm remembering to flush as well.

add_action('init','add_community_rewrite_rules');
function add_community_rewrite_rules()
{
add_rewrite_rule(
    'tag/(\d*)$',
    'index.php?pagename=community&stencil-tag=$matches[1]',
    'top'
);

add_rewrite_rule(
    'type/(\d*)$',
    'index.php?pagename=community&stencil-type=$matches[1]',
    'top'
);
}

If anyone sees the error of my ways here and can point me in the right direction I'd be incredible appreciative. Thanks.

0

The rewrite rules are wrong. For example, you want to rewrite from community/tag/easter to 'index.php?pagename=community&stencil-tag=easter', so the regex should contain community/tag/ and not only tag/. Also, \d match only digits but the tag value is a string. Same apply to the rewrite for stencil-tag. You could use . to match any character, both numeric and strings.

add_action('init','add_community_rewrite_rules');
function add_community_rewrite_rules() {
    add_rewrite_rule(
       '^community/tag/(.*)$',
       'index.php?pagename=community&stencil-tag=$matches[1]',
       'top'
    );

    add_rewrite_rule(
        '^community/type/(.*)$',
        'index.php?pagename=community&stencil-type=$matches[1]',
        'top'
    );
}

Although those rewrite rules can be correct, I think you should use another approach. Use the default archive page generated by WordPress for the projects post type. If you need a specific template for it, create the file archive-projects.php in your theme folder. You can rewrite the slug from projects to community when registering the post type, if you want that URL for the post type archive:

add_action( 'init', function() {

    $args = array(
      //Your args here
      'rewrite' => array( 'slug' => 'community' ),
    );
    register_post_type( 'projects', $args );

} );

Then, when registering the taxonomies, you can build the rewrite rules also:

add_action( 'init', function() {

    //Custom post type
    $args = array(
      //Your args here
      'rewrite' => array( 'slug' => 'community' ),
    );
    register_post_type( 'projects', $args );

    //Custom taxonomies: stencil-tag
   $args = array(
      //Your args here
      'rewrite' => array( 'slug' => 'community/tag' ),
    );
    register_taxonomy( 'stencil-tag', 'projects', $args );

    //Custom taxonomies: stencil-tag
    $args = array(
      //Your args here
      'rewrite' => array( 'slug' => 'community/type' ),
    );
    register_taxonomy( 'stencil-type', 'projects', $args );

} );

Now the rewrite rules are handled by WordPres automatically and you don't need to render a page and make a secondary query for the posts inside the page as they are fetched in the main query.

  • Thank you. Sincerely. My regex game is weak, I didn't even notice but your mods worked perfectly. I also appreciate the recommendation on proper approach. I'll probably end up going that direction but good knowledge all around. – RyanMac Mar 26 '15 at 13:05
  • The only issue now is that (and this is probably one of many reasons I should use the archive template) it breaks pagination. If I click to page 2, the URL is website.com/community/tag/easter/page/2/ and it returns an empty set even though there are more results to be shown. – RyanMac Mar 26 '15 at 14:49
  • The broken pagination is a common issue when using custom queries. There are several threads about that in this site. For example this one. Some times, rewrite rules for pagination may also be needed for custom queries, but not sure if this is your situation. I would need your pagination function to know it. – cybmeta Mar 27 '15 at 9:28

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.