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.

RyanMac
  • 103
  • 1

1 Answers1

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.

cybmeta
  • 20,668
  • 5
  • 47
  • 58