We are using CPT’s to manage a frequently asked questions page on a site, where the question is the post title and the answer is the post content. There is a main page for the FAQs that shows all posts (FAQ archive page). With this structure we really have no need for the single view for any FAQ and in fact would like to omit it from the site structure. To address permalinks we’d like to set them to be something like example.com/faq/#uniqueIdentifier, thinking that we’ll use the #uniqueIdentifier to match a div on the archive page containing the answer and call attention to it in some fashion. The uniqueIdentifier could be post ID, faq question title, data from a meta box, or something else.
So let me recap what I need we need to accomplish:
(1) rewrite the faq permalinks to be /faq/#something, and
(2) make sure all /faq/ links route to archive template and not single
I’m mostly a noob but pretty good at fumbling my way through things. Have never attempted any rewrites though so would appreciate some particular direction on that.
Thank you.
Hi @daxitude:
Let me first suggest you reconsider. If you don’t have individual FAQ pages for each FAQ:
You reduce your surface are for search engine optimization and reduce the potential traffic that you might get, and
You make it impossible for someone to share a specific FAQ with a friend over email and/or share with their network on Facebook, Twitter, etc. (As a user I’m always frustrated by site developers who disallow me to have a direct URL to an item and instead force me to link to the page that lists all items.)
However, if you still want to do so then do two things:
1.) Use the
'post_type_link'
hookUse the
'post_type_link'
hook to modify the URL like in the following example *(I’m assuming your custom post type is'faq'
). Add the following to your theme’sfunctions.php
file:2.)
unset($wp_rewrite->extra_permastructs['faq'])
This is a hack, but it’s a required hack to do what you want. Use an
'init'
hook tounset($wp_rewrite->extra_permastructs['faq'])
. It removes the rewrite rule thatregister_post_type()
adds. I’m including a call toregister_post_type()
so I can provide a complete example for both you and others:That’s about it.
Of course the above use of
$wp_rewrite->flush_rules()
in an'init'
hook is really bad practice and should really only be done once so I’ve implemented a complete and self-contained plugin calledFAQ_Post_Type
to do it right. This plugin adds a FAQ post type with the URL rules that you want and it uses aregister_activation_hook()
to flush the rewrite rules; activation being obviously one of the few things that requires plugin code instead of code that can run in a theme’sfunctions.php
file.Here’s the code for the
FAQ_Post_Type
plugin; feel free to modify for your requirements:You could also possibly kept the flush rules inside the
'init'
by using a check for an option value if you prefer this:Your choice.
Anyway, let me know if there are use-cases you discover that this does not address.