1

I would like to redirect to a post if the taxonomy term it belongs to has only one post assigned to it and so far I have this:

$term_id = get_queried_object()->term_id;
$taxonomy_name = 'product_range';
$term_children = get_term_children( $term_id, $taxonomy_name );

foreach ( $term_children as $child ) {
  $term = get_term_by( 'id', $child, $taxonomy_name );
    if($term->count <= 1 ) {
      echo '<a href="'. get_term_link($child, $taxonomy_name) .'" class="thumb" title="'.$term->name.'">'.$term->name.'</a>';
    }
}

This links through to the archive page but I want it to redirect the user to the relevant post, I am not sure what I need to do to change the permalink to go to the single post page.

5
  • Do you want to be redirected to the post from the category or do you want to output the link of this post in the category which would hardly make sense to me since your category should already include the post...?
    – ClemC
    Aug 14, 2017 at 14:53
  • I want to link through to the post from the taxonomy. Aug 14, 2017 at 15:06
  • Does it mean you don't want your user landing on the category page if it has only one post? In this case, it's called a redirection.
    – ClemC
    Aug 14, 2017 at 15:08
  • Yes I don't want the user to land on the Category page, I need them to be redirected to the post if only 1 post exists. Aug 14, 2017 at 15:12
  • OK then, see my updated answer...
    – ClemC
    Aug 14, 2017 at 15:15

2 Answers 2

0

I think you can simply use WP_Term's $count property which should hold the number of posts attached to this current term.
Then, if only one post is attached to this term, query this post object and do your stuff with it...

$term = get_queried_object();

if ( $term->count === 1 ) {
    $args = array(
        'tax_query' => array(
            array(
                'taxonomy'         => 'product_range',
                'field'            => 'term_id',
                'terms'            => array( $term->term_id ),
                'include_children' => false,
            ),
        )
    );

    $query = new WP_Query( $args );
    $posts = $query->posts;
    $post  = $posts[0];

    /**
     * IMPORTANT FOR SEO...
     * Temporary redirection until your category is populated - Use 301 instead of 302 to redirect permanently...
     */
    if ( wp_redirect( get_permalink( $post->ID ), 302 ) ) {
        exit;
    }
}
9
  • Unfortunately this doesn't redirect me to the post. Aug 14, 2017 at 15:19
  • @NeelamKhan, I updated my answer to change 'terms' => $term->term_id to 'terms' => array( $term->term_id ). Let me know if it works or not...
    – ClemC
    Aug 14, 2017 at 15:50
  • Unfortunately that doesn't fix it, I appreciate your help though. I am trying to get it to work. Aug 14, 2017 at 16:01
  • @NeelamKhan what is the output of var_dump( $term ); and var_dump( $posts ); ?
    – ClemC
    Aug 14, 2017 at 16:56
  • the output of var_dump($post); is array(0) { } and the output of var_dump($term); is all the taxonomy information. Aug 15, 2017 at 8:45
0

@ClemC I managed to figure this out by adding a WP_Query to query the posts and check the count, here is my code:

    $term_id = get_queried_object()->term_id;
        $taxonomy_name = 'product_range';
        $custom_terms = get_term_children( $term_id, $taxonomy_name );
            echo '<div class="row row__condensed">';
            foreach($custom_terms as $custom_term) {
                $term = get_term_by( 'id', $custom_term, $taxonomy_name );
                wp_reset_query();
                $args = array(
                    'post_type' => 'product',
                    'posts_per_page' => 1,
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'product_range',
                            'field' => 'slug',
                            'terms' => $term->slug,
                        ),
                    ),
                 );

                $loop = new WP_Query($args);

                if($loop->have_posts()) {

                    while($loop->have_posts()) : $loop->the_post();
                        // If only one post exists link to product
                        if($term->count === 1 ) {
                            echo '<div class="col-xs-12 col-md-4">';
                                echo '<div class="grid__item">';
                                    echo '<a href="'.get_permalink().'">';                                                       
                                    echo '</a>';
                                    echo '<h3><a href="' . get_permalink() . '">'.$term->name .'</a></h3>';
                                    echo '<p>'.wp_trim_words($term->description, 23, '...').'</p>';
                                echo '</div>';
                            echo '</div>';
                        }
                        //else link to the listing page
                        else {
                            echo '<div class="col-xs-12 col-md-4">';
                                echo '<div class="grid__item">';
                                    echo '<a href="' . get_term_link( $custom_term, $taxonomy_name ) . '">';                                    
                                    echo '</a>';
                                    echo '<h3><a href="' . get_term_link( $custom_term, $taxonomy_name ) . '">'.$term->name .'</a></h3>';
                                echo '<p>'.wp_trim_words($term->description, 23, '...').'</p>';
                                echo '</div>';
                            echo '</div>';
                        }
                    endwhile;
                }
            else {
        echo "no posts found.";
            } //endforeach
        echo '</div>'; 
1
  • It was probably not working because of the missing 'post_type' => 'product' arg in my answer...
    – ClemC
    Aug 15, 2017 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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