0

I was able to echo out all terms belonging to a custom taxonomy using the code below:

$args = array('post_type' => 'my_post_type','number' => '999');
$terms = get_terms( 'my_taxo',
          [
            'hide_empty' => true,
            'orderby' => 'wpse_last_word',
          ] , $args );

foreach ( $terms as $term ) { 
    echo '' . $term->name . ''; 
}

This code provides the results I want on my localhost (xampp). Basically, it outputs all of the terms assigned to a particular post. But when I upload the code to a live server, the code no longer works as expected. Instead, it shows all the terms without filtering them. I even updates the PHP version on my live server match to the local server; still no luck.

Can anyone point out the issue on my code.

4

Your code is wrong. I don't know how it is working in your localhost. Cause-

  1. You are calling get_terms() with 3 parameters which actually accepts 2 parameters. The last one is extra.
  2. And secondly get_terms() returns all the terms of a taxonomy, not the terms associated with a post.

For getting the terms associated for a post you can use wp_get_post_terms.

Usage of wp_get_post_terms inside WordPress loop-

For each post you'll get the post's terms by calling wp_get_post_terms like below-

//Do something if a specific array value exists within a post
$term_list = wp_get_post_terms($post->ID, 'your_taxonomy', array("fields" => "all"));
// Then you can run a foreach loop to show the taxonomy terms infront.
foreach($term_list as $term_single) {
    echo $term_single->slug; //do something here
}

And for outside of the loop-

// Do something if a specific array value exists within a post
// And somehow you need to get the post ID to pas it to below.
$term_list = wp_get_post_terms($post_id, 'your_taxonomy', array("fields" => "all"));
// Then you can run a foreach loop to show the taxonomy terms infront.
foreach($term_list as $term_single) {
    echo $term_single->slug; //do something here
}

Hope that helps.

| improve this answer | |
  • can you show me the script to use. – Roshan Deshapriya May 30 '17 at 13:39
  • Have you visited the link ? @rosh8111 – CodeMascot May 30 '17 at 13:40
  • Thanks, i'll check the codex and let you know if it works, thanks for pointing it out.- @the_dramatist – Roshan Deshapriya May 30 '17 at 17:35
  • Hi, I have tested all the scripts out there nothing out putting my term names of the post belongs to CPT. Can you be kind enough to publish the code that do the magic, please note my code is outside loop, I'm currently using $args = array('post_type' => 'projects','number' => '1'); $terms = wp_get_post_terms($post_id, 'Client',array("fields" => "names"), $args); foreach( $terms as $term ){ echo '<h5>' . $term->name . '</h5>'; } – Roshan Deshapriya May 30 '17 at 20:51
  • Will you please clear what you are trying to achieve ? Better you edit the question and clear it more ! @rosh8111 – CodeMascot May 30 '17 at 21:48
0

After few researchs I found below script do the work, thanks @the_dramatist pointing me to a right direction, I had use global $post, but not in a correct way

global $post;
$loop = new WP_Query(array('post_type' => 'myCPT', 'posts_per_page' => -1));
while ($loop->have_posts()) : $loop->the_post();
$terms = wp_get_post_terms($post->ID, 'my_taxonomy');                   
foreach($terms as $term_single) {
echo '' . $term_single->slug. '';
}
endwhile;

If anyone getting empty results after this, just delete the categories and re-create and apply them.

| improve this answer | |

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.