1

I have an URL formatted so:

http://domain.com/custom-post-type/category/cat-name/page/page-number

I need to extract:

cat-name and page-number

So that in my functions.php, I can create the custom rewrite rule:

function my_insert_rewrite_rules( $rules ) {
  $newrules = array();
  $newrules['projects/category/(.+)/page/(\d+)/?$'] = 'index.php?post_type=project&project_cat=$matches[1]&paged=$matches[2]';
  return $newrules + $rules;
}

I am trying to use the expression:

projects/category/(.+)/page/(\d+)/?$

Which seems to work appropriately (escaping the forward slashes) in this example: http://rubular.com/r/5rzECsTexy

However, this never seems to work, the URL:

http://domain.com/projects/category/print and http://domain.com/projects/category/print/page/2 always seem to 404.

My previous rewrite rule of projects/category/(.*/?)$ worked for the category, but did not pull the page number properly.

Edit Custom Post Type registration code (in my theme's function.php)

// Custom Post Types
add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'project',
        array(
            'labels' => array(
                'name' => __( 'Projects' ),
                'singular_name' => __( 'Project' )
            ),
        'public' => true,
        'has_archive' => true,
        'taxonomies' => array('category'),
        'rewrite' => array('slug' => 'projects')
        )
    );
}

1 Answer 1

0

You don't need a custom rewrite rule to handle this, just set your taxonomy rewrite slug like:

'rewrite' => array( 'slug' => 'projects/category/%project_cat%' )
4
  • This didn't seem to work. Is there a reason not to use rewrite rules, or maybe a reason why this suggestion didn't work?
    – waffl
    Nov 6, 2013 at 22:39
  • Did you flush your rewrite rules after switching? Maybe add your post type and taxonomy registration code to your question. It's easier to let WordPress automatically generate the rules, it takes care of both versions with/without pagination, and just keeps the code a bit cleaner.
    – Milo
    Nov 6, 2013 at 23:02
  • Yes I flushed the rewrite rules biter switching. I'll add the post type registration code to the question! I ended up using custom queries and rewrite codes after finding the default setup didn't handle the taxonomy (categories) or pagination properly in the archives.
    – waffl
    Nov 11, 2013 at 12:42
  • I see, you're using the built in category taxonomy and not a custom taxonomy. In that case, you need to change category base in Settings > Permalinks to projects/category. As for why they all seem to 404, that's an entirely different issue specific to built in taxonomies that has nothing to do with rewrites- by default those queries don't include custom post types. See this answer for the solution to that.
    – Milo
    Nov 11, 2013 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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