[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.
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:
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:
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.
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:
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: