Skip Navigation

[Resolved] Taxonomy View rendered in PHP shows no results found

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use PHP to loop over all the terms assigned to a post and display some formatted text using a custom field applied to the term.

Solution: Use the native WordPress method wp_get_post_terms to get all the terms associated with a specific post. Then use foreach to loop over the terms. Inside that loop, use the types_render_termmeta API to retrieve the custom field values and echo the formatted text string.

$term_list_module = wp_get_post_terms($download->ID, 'download_category');
foreach( $term_list_module as $term ) {
  $module_color = types_render_termmeta( "module-colour", array( "term_id" => $term->term_id) );
  echo "<span style='color:" . $module_color . ";'>".$term->name."</span>";
}

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://codex.wordpress.org/Function_Reference/wp_get_post_terms
http://php.net/manual/en/control-structures.foreach.php

This support ticket is created 6 years, 2 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 3 voices.

Last updated by davidR-12 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1096180

Hi Christian,

Thats great and works well. Perhaps you can help me with one more related thing. I have a view designed to display category terms. I am using a view instead of a category as I need to be able to colour each term depending on a color-picker custom field color assigned to it. This is working well but isnt working in my PHP file. The code I have is a simple shortcode to display the view:

echo do_shortcode ('[wpv-view name="module-terms-view"]');

It just displays "No items found". the view itself has a query filter of "Taxonomy is set by the current post"

Regards,

David

#1096214

Sounds like a context issue where the current post is something other than the expected current post when you call do_shortcode. You can test that by throwing in some debug code:

echo "Title test: " . get_the_title() . "<br />";
echo do_shortcode ('[wpv-view name="module-terms-view"]');

It should echo the title of the expected current post. If not, then you can try to modify the current post context in your code, or you can modify the View's filter to be filtered by term ID, provided by a shortcode attribute. Then you can pass whatever term IDs you want in a comma-separated string. It's best practice to use the render_view API to display a View in PHP.

$args = array(
    'title' => 'module-terms-view',
    'terms' => '123,456,789'
);
if( function_exists( 'render_view') ){
  echo render_view( $args );
}

https://toolset.com/documentation/programmer-reference/views-api/#render_view

#1098476

Hi Christian,

I got the view to pull in the correct terms using the code provided by you

$term_list_module = wp_get_post_terms($download->ID, 'download_category');
foreach( $term_list_module as $term ) {
  echo "<span style='color:$module_color;'>".$term->name."</span>";
}

However, I am trying to use a custom field that is attached to this particular category to colour each term. The code for $module_color is:

$module_color = types_render_termmeta( "module-colour", array() )

But that isnt pulling in any hex color code.

David

#1098811

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi David,

Christian is currently on a public holiday today but he will be back tomorrow to continue helping here.

Thanks,
Shane

#1099640

The types_render_termmeta shortcode needs to know which term you want to query for the custom field value. The term_id attribute can only be omitted in 2 cases - in the context of a term archive or in the context of a term View. This is neither of those contexts, so you must add term_id to the array of parameters like this:

$module_color = types_render_termmeta( "module-colour", array( "term_id" => $term->term_id) );

https://toolset.com/documentation/customizing-sites-using-php/functions/

#1100566

Hi Christian,

Thats not pulling in any information. I am using:

$module_color = types_render_termmeta( "module-colour", array( "term_id" => $term->term_id) );

and to display I am using:

$term_list_module = wp_get_post_terms($download->ID, 'download_category');
foreach( $term_list_module as $term ) {
  echo "<span style='color:$module_color;'>".$term->name."</span>";
}

But no color information is being brought in.

#1100681

Hmm, I don't understand how the two code snippets you provided are connected. It seems like the $module_color should be defined in the foreach loop, like this:

$term_list_module = wp_get_post_terms($download->ID, 'download_category');
foreach( $term_list_module as $term ) {
  $module_color = types_render_termmeta( "module-colour", array( "term_id" => $term->term_id) );
  echo "<span style='color:" . $module_color . ";'>".$term->name."</span>";
}

Am I misunderstanding what you want to accomplish? I may need to see more context in the code.

#1101378

Hi Christian,

Thank you for that, I think the issue was indeed the module variable needed to be defined in the loop. This is all now working well.

Regards,

David