admin管理员组

文章数量:1323150

I have registered three custom taxonomies. All three of these custom taxonomies are attached to my custom post type.

There is only one taxonomy out of the three registered, that I would like not accessible what so ever on the frontend of the website.

What is the best solution for this?

I have been playing around with specifying different arguments while registering the taxonomy, but nothing seems to work.

$args['show_in_nav_menus'] = false;
$args['query_var'] = false;
$args['public'] = false;

Should I just hook into template_redirect and do a is_tax() check? If it's the taxonomy I want disabled, just redirect to the custom post type archive?

I have registered three custom taxonomies. All three of these custom taxonomies are attached to my custom post type.

There is only one taxonomy out of the three registered, that I would like not accessible what so ever on the frontend of the website.

What is the best solution for this?

I have been playing around with specifying different arguments while registering the taxonomy, but nothing seems to work.

$args['show_in_nav_menus'] = false;
$args['query_var'] = false;
$args['public'] = false;

Should I just hook into template_redirect and do a is_tax() check? If it's the taxonomy I want disabled, just redirect to the custom post type archive?

Share Improve this question edited Apr 5, 2014 at 13:32 Michael Ecklund asked Apr 5, 2014 at 13:12 Michael EcklundMichael Ecklund 6,83213 gold badges68 silver badges110 bronze badges 4
  • 1 Which arguments? Explain "nothing seems to work"? What effect are you looking to achieve and what do you see instead? – s_ha_dum Commented Apr 5, 2014 at 13:14
  • Using that settings you are not able to query the taxonomy using urls... but nothing prevent to query taxonomy using a custom query (new WP_Query(...)) – gmazzap Commented Apr 5, 2014 at 13:26
  • I just don't want the URLs to work for that taxonomy... period. – Michael Ecklund Commented Apr 5, 2014 at 13:27
  • 1 @MichaelEcklund So, what you have done so far is enough. – gmazzap Commented Apr 5, 2014 at 13:58
Add a comment  | 

4 Answers 4

Reset to default 18

s_ha_dum's answer didn't work for me, but this did:

/**
 * Completely disable term archives for this taxonomy.
 * @param  string $taxonomy WordPress taxnomy name
 */
function kill_taxonomy_archive($taxonomy){

    add_action('pre_get_posts', function($qry) {

            if (is_admin()) return;

            if (is_tax($taxonomy)){
                $qry->set_404();
            }

        }

    );

}

WordPress taxonomy provides flexibilities to set its properties while registering it. To disable WordPress taxonomy archives, set "public" as "false" as shown in below example code. This will remove "View" action link from taxonomy screen and also redirect the user to website homepage when trying to access url of taxonomy manually.

Example

// Register Custom Taxonomy 
function custom_taxonomy() { 
  $labels = array( 
    'name' => _x( 'Taxonomies', 'Taxonomy General Name', 'text_domain' ), 
    'singular_name' => _x( 'Taxonomy', 'Taxonomy Singular Name', 'text_domain' ), 
    'menu_name' => __( 'Taxonomy', 'text_domain' ), 
    'all_items' => __( 'All Items', 'text_domain' ), 
    'parent_item' => __( 'Parent Item', 'text_domain' ), 
    'parent_item_colon' => __( 'Parent Item:', 'text_domain' ), 
    'new_item_name' => __( 'New Item Name', 'text_domain' ), 
    'add_new_item' => __( 'Add New Item', 'text_domain' ), 
    'edit_item' => __( 'Edit Item', 'text_domain' ), 
    'update_item' => __( 'Update Item', 'text_domain' ), 
    'view_item' => __( 'View Item', 'text_domain' ), 
    'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ), 
    'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ), 
    'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ), 
    'popular_items' => __( 'Popular Items', 'text_domain' ), 
    'search_items' => __( 'Search Items', 'text_domain' ), 
    'not_found' => __( 'Not Found', 'text_domain' ), 
    'no_terms' => __( 'No items', 'text_domain' ), 
    'items_list' => __( 'Items list', 'text_domain' ), 
    'items_list_navigation' => __( 'Items list navigation', 'text_domain' ), 
 ); 
    
 $args = array( 
  'labels' => $labels, 
  'hierarchical' => false, 
  'public' => false, // Set it to false, which will remove View link from backend and redirect user to homepage on clicking taxonomy link.
  'show_ui' => true, 
  'show_admin_column' => true, 
  'show_in_nav_menus' => true, 
  'show_tagcloud' => true, 
);  

I'm not sure why @chris-herbert wrote it the way he did, but if you add this to your function file, this will force any navigation to that archive to go to your 404 page.

add_action('pre_get_posts', 'kill_taxonomy_archive');
function kill_taxonomy_archive($qry) {

    if (is_admin()) return;

    if (is_tax('tax-slug')){
        $qry->set_404();
    }
}

To guarantee that your taxonomy is never queried on the front end, you could strip it from all front end queries.

add_action(
  'pre_get_posts',
  function($qry) {

    if (is_admin()) return;

    $kill = 'genre'; // kill this taxonomy

    $tax_query = $qry->get('tax_query');
    if (empty($tax_query)) return;

    $relation = false;
    if (isset($tax_query['relation'])) {
      $relation = $tax_query['relation'];
      unset($tax_query['relation']);
    }

    foreach ($tax_query as $k => &$tax) {
      if (isset($tax['taxonomy']) && 'genre' === $tax['taxonomy']) {
        unset($tax_query[$k]);
      }
    }

    if (1 < count($tax_query)) {
      $tax_query['relation'] = $relation;
    }

    $qry->set('tax_query',$tax_query);

  }
);

Using a closure should make the callback hard to remove.

You may also want to implement the redirect as you speculated but doing so wouldn't prevent secondary queries in page templates or widgets from retrieving the taxonomy or could cause trouble if you try to redirect late in a page load.

本文标签: custom post typesHow to completely disable a taxonomy archive on the frontend