Hi Mark!
Is there anyway to have a tag archive that only shows results for a specific CPT?
You can accomplish this by registering a separate, custom taxonomy that is only associated with your CPT. With code you would do it with the register_taxonomy function. There are also some plugins out there that will help you do this, like Custom Post Type UI.
@coreymckrill, I’ve got CPT UI running already for what I’ve done so far. And that solution occurred to me, too. The issue with using another custom taxonomy (instead of native tagging) is that the client has already attributed tons of tags to tons of existing posts. I didn’t want to make them do redundant work if they didn’t have to.
Thanks for the input!
I was about to post the same sort of solution (use a custom taxonomy) but preface it with “I hope you’re not so deeply into using tags that you can’t back up and do it right.” 🙂
It might be possible to use a pre_get_posts() filter on the tag archive page to set the post type to your CPT or ‘post’ based on the referrer, though I’m not sure how reliable or efficient that would be.
Another approach might be that you run a query to duplicate the existing tag terms in the new taxonomy, then run yet another query to change the tags attached to the CPT posts from the general tag taxonomy to your new one.
@sterndata, that’s SOOOO funny!! I saw a similar solution somewhere involving passing something like “?posttype=movie” in the tag link and doing the filtering based on that via a functions.php method.
I may just do the custom taxonomy and compel the client to redo the tagging. They got the cash.
Thanks!
Altering queries through pre_get_posts will work just fine. Where it might get tricky is when you want to get regular posts with the same tags. Like Steve suggests, checking the referer can work. It depends on where all the referring links come from and if the different kinds of requests can be discerned by the pre_get_posts callback. It’s only if there is absolutely no way to discern the difference where an alternate taxonomy would be required.
Of course, having a special taxonomy for your CPT makes good sense from a schema design standpoint. But you are well past schema design. Is it worth going back? Only you and your client can decide. If re-tagging is particularly onerous, you could write a one time script that goes through all the CPTs and replicates the current tags as new custom taxonomy terms, then removes the original tags. Finally, remove the CPT/tags relationship. As one time code, it doesn’t need to be pretty, nor have much of a UI. It shouldn’t take too long to put it together.