0

I'm trying to display a custom field value added to custom taxonomy on single custom post type. I have articles post type with custom taxonomy called issue. I used the code from Tax-Meta-Class to add custom field "publication date" to that taxonomy. For each issue I added a "publication date" value. To display the publication date of the child issue on each related single article I tried to use this code:

$object_terms = wp_get_object_terms( $post->ID, 'issue', array( 'fields' => 'all' ) );
if ( $object_terms ) {
    foreach ( $object_terms as $term ) {
        if ( $term->parent ) { 
            $t_id .= $term->term_id;
            $res .= get_tax_meta($t_id,'publication_date'); 
        }
    }
    echo $res;  
}

But nothing is displayed. I also tried to use get_term_meta instead of get_tax_meta but got nothing. If I added echo $t_id; It displays the term ID which means that the code can correctly get the term ID but it fails to display the custom field value. Any help please?

1
  • Where did you get the get_tax_meta() and get_term_meta() functions from?
    – Robbert
    Apr 17, 2016 at 20:41

2 Answers 2

1

Possible solutions:

  1. Check if there's a prefix which you setup for the custom field (https://en.bainternet.info/tax-meta-class-faq/#comment-1107)

  2. Go to the Tax-meta-class/Tax-meta-class.php file and find the function you're calling and debug the issue:

    // Tax-meta-class.php
    
    public function get_tax_meta($term_id,$key,$multi = false){
        $t_id = (is_object($term_id))? $term_id->term_id: $term_id;
        var_dump ($t_id); // debug this
    
        $m = get_option( 'tax_meta_'.$t_id);
        var_dump ($m); // debug this
    
        if (isset($m[$key])){
             return $m[$key];
        }else{
            return '';
        }
    }
    
  3. Check the wp_options table and search the option_name column for the option (the parameter of get_option('tax_meta_foo_bar') above). It's either not there or you're missing the prefix for get_option not to return a value.

2
  • thanks for your detailed answer. I tried option 1 & 2 but no results. I searched for what you said in wp_options & I didn't find it. what I should do please?
    – nisr
    Apr 17, 2016 at 22:28
  • please see how i fixed the problem with my answer below.
    – nisr
    Apr 17, 2016 at 22:51
0

Here what I did & finally problem is solved. To make the code work just use get_term_meta and add true to be like this:

    $res .= get_term_meta($t_id,'publication_date',true); 

I hope this will help any one facing such issue. Also, those who use prefix need to add it before the field name as mentioned by P-S

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.