Skip Navigation

[Resolved] Can not use toolset function inside functions php

This support ticket is created 3 years, 9 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 3 replies, has 2 voices.

Last updated by oleksandrM 3 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#2134761
toolset.png
plugin-load.png

Hello.

1. Why doesn't it work?
2. How can I use toolset functions like "toolset_get_related_posts" in functions.php?

3. If I use it like in the screen 3 toolset_get_related_posts return empty array, but without error!

ps: If I use it in post template like single-custom-type-slug.php -- all works fine.

#2134825

#3. is my fault.

#2135089

Hello, assuming the Toolset Types plugin is active and your site uses the new post relationships system (if the site was created using Types 3.0+, you are using the new post relationships system), this is probably a timing issue where the post relationships API function is called before Toolset is completely loaded. I use toolset_get_related_posts in functions.php quite often, but usually the function is called inside another hook or shortcode definition. The developers suggest using the post relationships API no sooner than init with a priority of 15 or higher:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#remarks

You may need to delay your code with an add_action hook, or in some other hook. Many examples here on the site use toolset_get_related_posts in shortcode definitions, or in callbacks for Forms API functions like cred_submit_complete, or in Views filter APIs like wpv_filter_query, etc:
https://toolset.com/forums/topic/next-and-previous-sibling-posts-in-a-one-to-many-post-relationship/
https://toolset.com/forums/topic/difference-between-wp_toolset_associations-and-wp_toolset_associations_old/
https://toolset.com/forums/topic/table-with-child-posts
https://toolset.com/forums/topic/repeatable-field-groups-form/

#2135399

My issue is resolved now. Thank you!