WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Type Category Archives (5 posts)

  1. Rebecca Schaefer
    Member
    Posted 1 year ago #

    I've been searching high and low for two days and none of the posts online have gotten me anywhere. The website in question has categories and four post types. The post types all share the categories. I have my CPT archives working just fine. site.com/cpt1/ works like a charm.

    What I now need is site.com/cpt1/category/categoryName/

    add_rewrite_rule('(cpt1|cpt2|cpt3|cpt4)/category/([^/]+)/?',
             'index.php?category_name=$matches[2]&post_type=$matches[1]');

    This is what I'm currently trying without any success whatsoever no matter how I tweak it.

  2. bcworkz
    Member
    Posted 1 year ago #

    Hi Rebecca, a wild guess, could your category request be getting matched to the generic CPT rewrite and handled before the category rewrite even gets a chance?

    If that's not it, you could try hooking the filter 'posts_request_ids' just to see how the query ended up. A good clue as to where things went wrong.

    If all else fails, you could massage the query via one of the other 'posts_*' filters, but the parser would at least need to recognize it's a category request to set the is_category() tag. If things get really desperate, you could create your own handler.

    Good luck tracking this down,
    bc

  3. Rebecca Schaefer
    Member
    Posted 1 year ago #

    Thanks for the input, bc. I think you're right. I'd tried adding higher priority to my rule, and it broke all the archives.

    I finally got it to work by appending the CPT to the end of the category rewrite.

    add_rewrite_rule('category/([^/]*)/([^/]*)/?',
    	'index.php?category_name=$matches[1]&post_type=$matches[2]',
    	'top');

    The only drawback here is now, within the categories, the post type must be identified by name rather than slug. Some of them have custom slugs - ice_cream may have the slug delicious_frozen_goodness, for example. (Personally not too happy about this, it was a cosmetic change requested halfway into the project.) The archive for the CPT will be accessed directly
    site.com/delicious_frozen_goodness/

    But the archive for the category/CPT combo needs to be accessed as
    site.com/category/new_treats/ice_cream/

    Any thoughts on the rewrite there? I'm guessing it's something extremely obvious that I'm missing.

    I'm also experiencing a few ancillary issues now, including the global conditional statements not always working. For example, is_front_page(), is_home(), is_single(), etc aren't operating as expected. I'm going to venture a guess that this is because of previous code I still have active that muddied up the system, so a code clean-up is in order.

    Thanks again!

  4. bcworkz
    Member
    Posted 1 year ago #

    Sorry, no easy answers from me, I often can't see where the problem is without seeing the result. Either the parser is not parsing the way you expect, or the query is not getting the query vars it expects, so is building the wrong query. The later would explain why the is_*() tags aren't working.

    You can look at the query vars by hooking the 'request' filter and doing a var_dump(); die();. That'll give you a clue where the problem lies, as this is after the parser but before the query is built.

  5. Rebecca Schaefer
    Member
    Posted 1 year ago #

    It was a collision with a function. I'd kind of screwed around with making it work before realizing I'd missed an obvious solution, so my code was...messy.

    New problem: RSS links don't work with that rewrite rule.

    I've edited it to this

    add_rewrite_rule('([^feed/]*)/category/([^/]*)/([^/]*)/?',
    	'index.php?category_name=$matches[2]&post_type=$matches[3]',
    	'top');
    add_rewrite_rule('feed/([^/]*)/category/([^/]*)/([^/]*)/?',
    	'index.php?feed=$matches[1]&category_name=$matches[2]&post_type=$matches[3]',
    	'top');

    In the hopes it would work, but, alas. Nothing. I'm going to try using rewrite endpoints and just appending the CPTs to that.

    Ideally, the permalink structure would look like this
    site.com
    site.com/cpt/
    site.com/cpt/feed/rss2/ (here is where everything stops working)
    site.com/cpt/category/category-slug/
    site.com/cpt/category/category-slug/feed/rss2/

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.