I have a custom post type with a custom taxonomy. I don't link to the taxonomy on the page but from the admin anyone can visit the taxonomy/term url and the unstyled list page will be visible. How can I force wp to not use the default layout (index.php) for that listing? What's the best method? For example I creat a taxonomy template and I redirect it to the front page?
2 Answers
If you just want to "turn off" the public facing part of a taxonomy (eg. just use it for grouping), you can do that when you register it.
Just set the query_var
argument to false
and WordPress will not recognize taxonomy page requests and simply 404. This will work if you don't have pretty permalinks enabled.
<?php
add_action('init', 'wpse94193_register');
function wpse94193_register()
{
register_taxonomy('your_taxonomy', 'your_post_type', array(
// other stuff here...
'query_var' => false,
));
}
You can also disable pretty permalinks by setting the rewrite
argument to false (props otto).
<?php
add_action('init', 'wpse94193_register');
function wpse94193_register()
{
register_taxonomy('your_taxonomy', 'your_post_type', array(
// other stuff here...
'rewrite' => false,
'query_var' => false,
));
}
If you do need to allow logged in users to view the taxonomy pages (can't tell from the question), that's different story.
-
1This. Also, set the
rewrite
arg to false as well to disable the permalinks for them too.– OttoCommented Apr 1, 2013 at 23:28 -
setting
query_var
to false only works when pretty permalinks aren't enabled, you need therewrite
argument as well to cover all bases. deleting my answer and upvoting yours :)– MiloCommented Apr 1, 2013 at 23:47
Check out the Template Hierarchy. If your custom taxonomy is called custom_taxonomy
, you should be able to create a template file called taxonomy-custom_taxonomy.php
, and it could contain, for instance:
<?php
wp_redirect( home_url() );
exit; // stop processing since we're redirecting
?>
(See the Codex page for wp_redirect()
for more on redirecting in WordPress.)
-
Yes it was my idea too, what to you think is it the best method?– hepii110Commented Apr 1, 2013 at 21:16
-
I'd say go with @chrisguitarguy's method -- set the
rewrite
andquery_var
to false. My way will work, but his way looks like it's the proper WordPress way to do it.– Pat JCommented Apr 2, 2013 at 13:41