Skip Navigation

[Resolved] How to code and get field value from a category

This support ticket is created 3 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by fred-r.M 3 years, 10 months ago.

Assisted by: Jamal.

Author
Posts
#1608223

Tell us what you are trying to do?

I have custom types and categories for them. How can I get the "values" of those categories? I use them in a function, where I get a short code then.

Is there any documentation that you are following?

Not found anything useful

Is there a similar example that we can see?

This is the function I am using:

function display_tourdetails() {

global $post;
$str = '';

$tour_duration_category = get_post_meta($post->ID,'wpcf-tour-duration-category',true);
$tour_duration = get_term_meta($post->ID,'wpv-tour-duration',true);
$tour_specialty = get_term_meta($post->ID,'wpv-tour-specialty',true);
$total_tour_distance_km = get_post_meta($post->ID,'wpcf-total-tour-distance-km',true);
$total_tour_distance_mi = get_post_meta($post->ID,'wpcf-total-tour-distance-mi',true);
$average_distance_per_day = get_post_meta($post->ID,'wpcf-average-distance-per-day',true);
$average_climbing_per_day = get_post_meta($post->ID,'wpcf-average-climbing-per-day',true);
$price = get_post_meta($post->ID,'wpcf-price',true);

$str .= '<div class="facts">';
if(!empty($tour_duration_category)){
$str .= '<div class="style touring">Tour Cat:<br /> '.$tour_duration_category.'</div>';
}
if(!empty($tour_duration)){
$str .= '<div class="duration">Duration:<br /> '.$tour_duration.'</div>';
}
if(!empty($tour_specialty)){
$str .= '<div class="destination">Difficulty:<br /> '.$tour_specialty.'</div>';
}
if(!empty($tour_duration_category)){
$str .= '<div class="price">Specialty:<br /> '.$tour_duration_category.'</div>';
}
if(!empty($total_tour_distance_km)){
$str .= '<div class="style touring">Total Riding Distance:<br /> '.$total_tour_distance_km.' km / '.$total_tour_distance_mi.' miles</div>';
}
if(!empty($average_distance_per_day)){
$str .= '<div class="duration">Ride Distance p/day:<br /> '.$average_distance_per_day.'</div>';
}
if(!empty($average_climbing_per_day)){
$str .= '<div class="destination">Av. climbing p/day:<br /> '.$average_climbing_per_day.'</div>';
}
if(!empty($price)){
$str .= '<div class="price">Price:<br /> '.$price.'</div>';
}
$str .= '</div>';
return $str;
}
add_shortcode( 'show_tourfacts', 'display_tourdetails' );

What is the link to your site?

hidden link

#1608863

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello and thank you for contacting the Toolset support.

Categories are taxonomies in WordPress vocabulary. https://toolset.com/glossary/taxonomy/
To get a taxonomy term of a post from the database we can use the function wp_get_post_terms. Read more about it here https://developer.wordpress.org/reference/functions/wp_get_post_terms/

In Toolset, taxonomies are not prefixed by 'wpcf-'. We can get the term ids for a taxonomy Category(slug: category) with the following:

wp_get_post_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );
#1609291

Dear Jamal

Okay, I understand so far.

Do You have an example how to do it? Meaning in the whole "context":

We have 3 different taxanomies (tour-duration, tour-specialty & tour-difficulty) as categories. As You can see above in my php code

I have at the moment this (I understand that this is wrong respectively not working):

$tour_duration = get_term_meta($post->ID,'wpcf-tour-duration',true);
  $tour_specialty = get_term_meta($post->ID,'wpcf-tour-specialty',true);
  $tour_difficulty = get_term_meta($post->ID,'wpcf-tour-difficulty',true);

and later this for the category "duration"

if(!empty($tour_duration)){
       $str .= '<div class="duration"><strong>Duration:</strong><br /> '.$tour_duration.'</div>';

How I get now only one - so far we are having only one category per post type each! - with this?

$tour_duration = wp_get_post_terms( $post->ID, 'tour-duration', array( 'fields' => 'names' ) );
#1612563

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello,

Please note that "get_term_meta" takes a $term_id instead of a $post_id. Also it returns an array instead of a string.
https://developer.wordpress.org/reference/functions/get_term_meta/

If you want to use get_term_meta, you will need to get the term_ids then get the term meta from the term ids:

$term_ids = wp_get_post_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );
$term_durations = array();
foreach($term_ids as $term_id) {
  $term_duration = get_term_meta($term_id,'wpcf-tour-duration',true);
  $term_durations[] = $term_duration
}

You may also use directly "wp_get_post_terms" specifying the term meta as follow:

wp_get_post_terms( $post->ID, 'category', array( 'fields' => 'wpcf-tour-duration' ) );

Please note that I did not test any of the following codes. Let me know what you will get and if you still encounter issues, I might create a local test and check how to get it.

#1613293

I have this for the moment, what gives me the solution I need:

function display_tourdetails() {
  
  global $post;
  $str = '';
   
  $tour_duration_category = get_post_meta($post->ID,'wpcf-tour-duration-category',true);
  //$tour_duration = get_term_meta($post->ID,'wpcf-tour-duration',true);
  $atour_duration = wp_get_post_terms( $post->ID, 'tour-duration', array( 'fields' => 'names' ) );
  $tour_duration = $atour_duration[0];
  //$tour_specialty = get_term_meta($post->ID,'wpcf-tour-specialty',true);
  $atour_specialty = wp_get_post_terms( $post->ID, 'tour-specialty', array( 'fields' => 'names' ) );
  $tour_specialty = $atour_specialty[0];
  //$tour_difficulty = get_term_meta($post->ID,'wpcf-tour-difficulty',true);
  $atour_difficulty = wp_get_post_terms( $post->ID, 'tour-difficulty', array( 'fields' => 'names' ) );
  $tour_difficulty = $atour_difficulty[0];
  $total_tour_distance_km = get_post_meta($post->ID,'wpcf-total-tour-distance-km',true);
  $total_tour_distance_mi = get_post_meta($post->ID,'wpcf-total-tour-distance-mi',true);
  $average_distance_per_day = get_post_meta($post->ID,'wpcf-average-distance-per-day',true);
  $average_climbing_per_day = get_post_meta($post->ID,'wpcf-average-climbing-per-day',true);
  $price = get_post_meta($post->ID,'wpcf-price',true);
  
  $str .= '<div class="facts">';
  if(!empty($tour_duration_category)){
       $str .= '<div class="style touring"><strong>Tour Cat:</strong><br /> '.$tour_duration_category.'</div>';
   }
   if(!empty($tour_duration)){
       $str .= '<div class="duration"><strong>Duration:</strong><br /> '.$tour_duration.'</div>';
   }
   if(!empty($tour_difficulty)){
       $str .= '<div class="destination"><strong>Difficulty:</strong><br /> '.$tour_difficulty.'</div>';
   }
   if(!empty($tour_specialty)){
       $str .= '<div class="price"><strong>Specialty:</strong><br /> '.$tour_specialty.'</div>';
   }
   if(!empty($total_tour_distance_km)){
       $str .= '<div class="style touring"><strong>Total Riding Distance:</strong><br /> '.$total_tour_distance_km.' km / '.$total_tour_distance_mi.' miles</div>';
   }
   if(!empty($average_distance_per_day)){
       $str .= '<div class="duration"><strong>Ride Distance p/day:</strong><br /> '.$average_distance_per_day.'</div>';
   }
   if(!empty($average_climbing_per_day)){
       $str .= '<div class="destination"><strong>Av. climbing p/day:</strong><br /> '.$average_climbing_per_day.'</div>';
   }
   if(!empty($price)){
       $str .= '<div class="price"><strong>Price:</strong><br /> '.$price.'</div>';
   }
   $str .= '</div>';
  return $str;
}
add_shortcode( 'show_tourfacts', 'display_tourdetails' );
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.