Skip Navigation

[Resolved] How to set a certain term in a custom Taxonomy when creating a custom post?

This support ticket is created 6 years, 8 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.

This topic contains 1 reply, has 2 voices.

Last updated by Beda 6 years, 8 months ago.

Author
Posts
#630188

Hi,

I need to set automatically a certain term in the standard taxonomy when creating a new custom post type. How can this get achieved?

Example: I have custom post type "books" and need to set the term "review" in the standard taxonomy. As this has to be done wit every "book" it is tedious to do so manually - an can get forgotten.

Further: if it is possible to set a certain term in the standard taxonomy in respect of a certain setting of a custom post field in the custom post type - the better!

Any suggestions? I think some PHP code will get involved. Can you give me some lins for that so that I can go further on from this starting point?

Kind regards, Martin

#630221

1. How do you create or update those posts?
(Backend, CRED?)
2. Where from should the data come?
3. Where to should it be updated, and when?

These are the questions that must be asked always in such a scenario.

According to your description, I think this will happen in the backend and no CRED is involved.
Data shall come from values existing in the post (post field) and some hardcoded data (terms you set) and shall update the terms of the new post only.

In the backend you will hook your Custom PHP code to "save_post()":
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

In this code you will:
- get the custom field value of your desire with "get_post_meta()":
https://developer.wordpress.org/reference/functions/get_post_meta/
- with conditional logic (IF/ELSE) in the PHP code you decide what to do depending on the above-returned value
- then, if the conditions match, you use "wp_set_post_terms()" to update the posts terms:
https://codex.wordpress.org/Function_Reference/wp_set_post_terms

If you require elaborated help with Custom code, we can always suggest the contractors as well:
https://toolset.com/contractors/