1

Is it possible to automatically redirect a child taxonomy to its parent, therefore not having a public page at all for the child taxonomy?

Example:

I have a taxonomy called venue, this is set up with 'hierarchical' => true,. This allows me to add venues which have been renamed for sponsorship purposes (eg: City Theatre becomes the XZY City Theatre, then ABC City Theatre, they are all the same building/location just different names).

Therefore if you clicked on a link for a child taxonomy (XZY City Theatre) you'd be taken to https://domain.com/venue/city-theatre NOT https://domain.com/venue/xyz-city-theatre.

Venue Taxonomy

This is my code for creating the taxonomy itself

add_action( 'init', 'gd_tax_venues', 0 );

function gd_tax_venues() {

    $plural = 'Venues';
    $single = 'Venue';
    $slug = 'venue';

    $labels = array(

        'name' =>  $plural,
        'singular_name' =>  $single,
        'menu_name' =>  $plural,
        'all_items' => 'All' . $plural,
        'parent_item' => 'Parent ' . $single,
        'parent_item_colon' => 'Parent ' . $single . ':',
        'new_item_name' => 'New ' . $single,
        'add_new_item' => 'Add New ' . $single,
        'edit_item' => 'Edit ' . $single,
        'update_item' => 'Update ' . $single,
        'view_item' => 'View ' . $single,
        'separate_items_with_commas' => 'Separate ' . $plural . ' with commas',
        'add_or_remove_items' => 'Add or remove ' . $single,
        'choose_from_most_used' => 'Choose from the most used',
        'popular_items' => 'Popular ' . $single,
        'search_items' => 'Search ' . $single,
        'not_found' => 'Not found',
        'no_terms' => 'No' . $single,
        'items_list' => $single . ' list',
        'items_list_navigation' => $single . ' list navigation',

    );

    $args = array(

        'hierarchical' => true,
  'labels' => $labels,
        'show_ui' => true,
        'meta_box_cb' => false,
  'show_admin_column' => true,
  'update_count_callback' => '_update_post_term_count',
        'query_var' => true,
        'public' => true
        
    );
    register_taxonomy( $slug, array( 'gig' ), $args );

}

taxonomy-venue.php

This is the code for the taxonomy page itself.

The parent shows all the posts which are assigned to both the parent and children (This is what I want displayed for all).

  $terms = wp_get_post_terms( $post->ID, 'venue' ); 
    $terms_ids = [];
    foreach ( $terms as $term ) {
      $terms_ids[] = $term->term_id;
    }
    $args = array(
      'post_type' => 'gig',
      'tax_query' => array(
      'relation' => 'AND',
        array(
          'taxonomy' => 'venue',
          'field'    => 'term_id',
          'terms'    => $terms_ids
        )
      ),
    );
    $query = new WP_Query($args);
    if ( $query->have_posts() ) { ?>
    <ul>
      <?php 
        while ( $query->have_posts() ) {
          $query->the_post();?>
      <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      <?php } ?>
    </ul>
  <?php }

If the redirect isn't possible, how can I show the parent and sibling posts on all child pages? and possibly change the canonical link on child pages to point to the parent page?

Edit: Redirect works with

Working based on the answer from mrcodefinger. Adding the following to the top of 'taxonomy-venue.php'

    $term = get_queried_object();
    
$parent = ( isset( $term->parent ) ) ? get_term_by( 'id', $term->parent, 'venue' ) : false;
    
    if( $parent ) {
      $url = get_term_link($term->parent);
      wp_redirect( $url, '301' );
    } 
1
  • taxonomies don't have parent/child relationships, are you sure you didn't mean parent terms?
    – Tom J Nowell
    Feb 4, 2021 at 10:47

1 Answer 1

1

You can check if your $term have parent and then redirect using wp_redirect()

<?php
    $term = get_queried_object();
    $parent = ( isset( $term->parent ) ) ? get_term_by( 'id', $term->parent, 'types' ) : false;

    if( $parent ) {
        wp_redirect($term->parent);
    } 
?>

I hope this helps!

Greetz

2
  • Legend, got it working based on the advice you gave to use wp_redirect(), updated the question with the solution as well for others - Thank you Feb 4, 2021 at 11:39
  • 1
    Cool, you're welcome! Feb 4, 2021 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.