Skip to content
Advertisement

Get taxonomy name

I’m trying to modify a theme that has custom posts with custom taxonomies. At the bottom of the posts it displays a list of other posts (as in “you might also like…”) What I’m trying to do is to filter the posts that are displayed in that area so they match the category of the current post. It’s a tour company, I want to display tours on the same area.

The custom posts have a term/taxonomy (I still don’t get this) called “st_tour_type” how can I get the name or value of the current post so then I can filter the ones display in the bottom of the page using that one as a reference?

At the moment I’m able to filter them manually if input the slug/name for “st_tour_type” but this doesn’t make sense

$args = [                    
   'posts_per_page' => 4,
   'post_type' => 'st_tours',
   'st_tour_type' => 'lerwanda',
]; 

I hope you get what I need, it’s my first time working with taxonomies…

Update (23Nov): Here’s more of the original code

$search_tax_advance = st()->get_option( 'attribute_search_form_tour', 'st_tour_type' );
$terms_posts = wp_get_post_terms(get_the_ID(),$search_tax_advance);
$arr_id_term_post = array();
foreach($terms_posts as $term_post){
   $arr_id_term_post[] = $term_post->term_id;
}        
$args = [                    
   'posts_per_page' => 4,
   'post_type' => 'st_tours',
   'post_author' => get_post_field('post_author', get_the_ID()),  
   'post__not_in' => [$post_id],
   'orderby' => 'rand',
   'tax_query' => array(
      'taxonomy' => $search_tax_advance,
      'terms' => $arr_id_term_post,
      'field' => 'term_id',
      'operator' => 'IN'
    ),
];

Advertisement

Answer

I don’t fully understand your question, so I’m not sure if this is what you expect, but try this. You need to get the term of your current post, by using the function get_the_terms. Here’s how you should do:

<?php 

global $post

$taxonomy = 'your_custom_taxonomy_slug';
$term = get_the_terms( $post->ID, $taxonomy ); 

The functions above will return an object that contains all the informations about your post’s current term, something like this:

<?php
 
Array
(
    [0] => WP_Term Object
        (
            [term_id] => 1
            [name] => Term Title
            [slug] => term-title
            [term_group] => 0
            [term_taxonomy_id] => 1
            [taxonomy] =>  custom_taxonomy
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )

)

Now according to your question, I guess you’re trying to make a query to get all related posts that in the same term. So the $args should be changed to:

<?php

$args = array(
    'posts_per_page' => 4,
    'post_type' => 'st_tours',
    'tax_query' => array(
         'taxonomy'     =>      $taxonomy,
         'field'        =>      'id',
         'terms'        =>      array( $term->term_id ),
         'operator'     =>      'IN',
    ),
);

See WP Docs for Taxonomy Parameters in Wp Query.

Oh and I see that you’re a little confused with those terms/taxonomies, here’s how you can understand it: Category in Post is a Taxonomy, and all categories that you create – each of them is a term. For custom post type (in your case it’s a Tour post type), let’s say you create a custom taxonomy called Countries, then you add some countries: US, Canada, Japan. So now each country is a term.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement