Skip Navigation

[Resolved] Get Custom Term Field data on an archive template

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

Problem:
I want to render data of my Taxonomy Term Fields created with Toolset on a Taxonomy Term Archive.

How can I do that?

Solution:
You can use either get_term_meta() or types_render_termmeta(), to render the meta fields of a term.

The most important is not how you render it but what data you pass to the render functions, as that is often the core cause for the above 2 methods "not working".
In this ticket for example the problematic detail was the ID passed to the function (the ID is supposed to be the Term ID).
On Archives, having many posts, you can't get that ID with the classic single item methods WordPress offers but need to use the get_queried_object_id() function for example.

Relevant Documentation:
https://toolset.com/documentation/user-guides/custom-content/displaying-wordpress-term-fields/#displaying-a-field-for-a-specific-taxonomy-term
https://developer.wordpress.org/reference/functions/get_term_meta/
https://developer.wordpress.org/reference/functions/get_queried_object_id/

This support ticket is created 4 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 2 replies, has 2 voices.

Last updated by martinE-4 4 years, 2 months ago.

Assisted by: Beda.

Author
Posts
#1521895

I am trying to access the contents of my image taxonomy custom field. I want to get the raw URL and give it to a hook I am writing in GeneratePress Premium element so that the image can be displayed in my header for the category archives. (I have a custom image for each category archive term 'asc')

My archive slug is 'asc'
My taxonomy custom field slug is 'asc-banner-image'

So far I tried:

$url_image = get_term_meta( get_queried_object_id(), 'wpcf-asc-banner-image', true);

but that seems to return a blank.

I also tried

$url_image = types_render_termmeta ( "asc-banner-image", array( "output" => 'raw' ) );

but that seems to return a blank also.

I am executing this hook on the category archive page in my archive template "Archive for ASC" and I know it is firing because I echo various troubleshooting messages to the screen as it executes. But the echo of $url_image is blank?

#1522381

Can you check what you receive when you change the non-Toolset part of this code to a hardcoded value?

$url_image = get_term_meta( '8', 'wpcf-asc-banner-image', true);

Note that "8" would need to be changed to the specific Term you want the data of.
You can also use error_log(print_r($var_to_analyse, true)); in your code, wherer $var_to_analyse is the variable you want to debug.
In this case you want to know if get_queried_object_id() is indeed the ID you expect.

If it is the right ID, then, the get meta functions should as well return that data appended to, if any data is saved.
But I suspect it is not, which would explain why types_render_termmeta() breaks, which takes the ID of the term automatically and lets you specify which FIELD to display of that term.
https://toolset.com/documentation/user-guides/custom-content/displaying-wordpress-term-fields/#displaying-a-field-for-a-specific-taxonomy-term
(PS I have already reported the syntax parsing errors on that Documentation page)

You can also pass the ID there, using the term_id parameter in the second function parameter array.
But you need to be sure the ID is the right one, which here seems to be not the case?

#1522797

Hi Beda
Thanks for the tip about error_log(print_r($var_to_analyse, true)); - it is cleaner looking at that in the Console error log than echoing on the top of my page.

When using the correct Term ID (in this case 33), the $url_image returns the correct url with the get_term_meta statement.

When I use error_log(print_r(get_the_ID(), true)); to see what get_the_ID() is holding , it says 1506 which is the ID for the first post in my list of posts. This is a category archive page and this post is the first one in the archive, not the id of the actual taxonomy term!

This led me to look for the proper WP core function to retrieve the term ID:

$url_image = get_term_meta( get_queried_object_id() , 'wpcf-asc-banner-image', true);

So get_queried_object_id() is the correct function to use and it returns the correct value of 33 in this case.

Many, many thanks for pointing me in the right direction and solving this problem Beda! I really appreciate it!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.