Can you use the same slug for a Post Type and Taxonomy?

Hi!

I'm wondering if you can use the same slug and post name for a post type and taxonomy. I'd like visitors to be sent to the post-type if one exists and if it doesn't exist, they should be sent to the taxonomy.php page. I am testing it out and I get a 404 page when it should be sending the visitor to taxonomy.php.

My Example:
I'm using the same slug for a taxonomy called "directors" and a post-type called "directors." I have a list of directors at the end of a news article: Jane Doe, John Doe.

John Doe submitted information for our post-type "directors" so when visitors click on his name they go to the post-type at mysite.com/directors/john-doe

Jane Doe did not submit information so when visitors click on her name they go to the taxonomy.php mysite.com/directors/jane-doe

As I mentioned, when you click on Jane Doe I get a 404 page instead of a taxonomy.php page.

I've been searching for an answer but can seem to find it. Do I need to use a template_redirect?

Thanks!

  • Mark Wallace

    Hi Arun! Hi Operapreneur!

    Sorry to jump in here Arun, but i just spent a few days looking at WP taxonomy vs. drupal taxonomy.

    Not to be little your ability/knowledge Operapreneur! As i said; I had to install drupal today just to relearn and compare Drupal Taxonomy to WP Taxonomy

    "Taxonomy" is like a file cabinet.
    "Categories" are like the "green folders" that hold the "yellow folders".
    "Sub-Categories" are like "yellow folders" that goes in the "green folders".
    "Tags" are like the labels you see on the folders.

    You create “Taxonomy" by setting up the "categories" and "tags".
    So if Jane Doe has not submitted her information, she should not be anywhere for a person to click her name.

    Also i do not think anyone should go to a taxonomy php. That would be like having them set up a new file cabinet when there is already a folder in the existing file cabinet.

    You can set this up many ways; forms, directory plugin, classifieds plugin all allow users to submit information to the proper Folder/tag for the front end, and “Custom Press” for the Backend.

    I have included Snip Shots i used to figure this mess out, they are of drupal, but if you compare the two snip shots you willget it!

    @Arun If I missed this please correct me; I just started setting my own taxonomies with this concept. lol

    Thanks Fella’s, And I am sorry if I missed this one!

  • Erik

    Hey Operapreneur,
    Having them both use the same slug will create conflicts, so why not use one plural and one singular?

    You could setup a system on the taxonomy template file that checks if their is a post, and if so modifys the loop to that data, and includes the appropriate template file. And if not, continues to the normal contents of the texonomy-director(s).php template file

    Once you have setup your list of taxonomies 'jane, joe, etc' that link into the taxonomy page 'taxonomy-director(s).php' then run a check to see if a post id can be found from slug (indicating a post entry is available), then inside the 'true' if branch, modify the query to setup this post data, then use an include to your single-director(s).php template file.. THEN move the rest of the template.php file into the 'false' branch of our check.

    This way, we arrive at taxonomy.php from you tax list, check if there is post data found if so set it up and off we go, if not keep doing the normal taxonomy.php stuff.

    I do something similar to use varying single.php template files based on post data, you could probably swing something similar to route from taxonomy template file to single template file if post data has been entered..

    Anyway, if you have questions I'll be around..

  • Operapreneur

    Hi All! Thanks for all the responses, they were all helpful.

    Arun: I guess I wasn't clear in my example. The user actions were based on their visit to the site and what they clicked on. Yes, I have full control over the slug names.

    MTB1701: Thanks for the breakdown. When I mentioned tax.php what I really meant was tax-'slug-name'.php. From what I understand via the template_hierarchy in the WP Codex, WP decides which template file is loaded and if tax.php does exist it would be loaded. I was hoping WP would load tax.php, category.php, tag.php, or index.php but instead I was getting a 404. In any case, the point is mute because Erik has presented a way around it.

    Erik: Thank you, thank you, thank you. I'm a newbie to PHP so, I can't exactly see the code that you describe but I understand the concept. It sounds like a perfect fix to my issue. I found some code from my old version of the site that I think is relevant. I know there is a lot missing from what you described (running a check for ID and the TRUE and FALSE branch but am I on the right track? Do you know where I could find some more direction? Many THANKS!

    $predef = get_defined_vars();
    	if (substr($predef["GLOBALS"]["_SERVER"]["SCRIPT_URL"], 0, 30) == "/director/")
    	{
    		include("singe_director.php");
    		exit();
    	}
  • Erik

    @Operapreneur
    Sorry for the delay, I've been busy with some membership integration stuff...

    You have successfully created the listings and links using the taxonomy. Now why not modify the taxonomy-director.php page (that your links are taking you too) to check if their is a post for that director and display that? you could even then go back into the normal taxonomy file routine and list out the rest of the articles that that tax is linked to..

    All you would need to do is open up the template page your landing on (im going to assume its called taxonomy-director.php for this), under the header call use the get_posts function with the 'by slug' method to determine if there is in fact a post that corresponds with that specific director. If you do get a post, setup the post data and display it..

    Oh ya, get the slug to check against by getting the slug for that taxonomy, they're both the same name, so checking for a post_type of 'director' or whatever with that slug should give you the corresponding post/director if they've name/generated-slug is the same between the two.

    You could even go farther and put the check above the get_header call, and instead of setting up and displaying the post, you could use a header redirect to take you to the single-director.php page and the query posts to setup the post data there..

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.