Skip Navigation

[Resolved] Auto fill custom field with category ID

This support ticket is created 4 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.

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)

Author
Posts
#1321089

As per the subject, I have a custom category field (catid).

Can I have it automatically input the category ID when a new category is created? I will need to edit the number when necessary

#1321091

I understand you have a Custom Taxonomy Field, attached to the Taxonomy "Category", named "catid", and want that field to get updated for each term added, with the ID of the term just created.

This is not possible with Toolset or WordPress out of the box.
You'd need to create a custom logic, allowing to grab the newly created Term's ID (and only when it's newly created, as afterwards you may edit it), then put that ID into the field for that term and update it as well.

I need to know where precisely this all should happen?
This could be in a Toolset Form, or when adding the term in the backend of term lists, or when adding the term editing the post.

In any case Toolset cannot solve the issue, but I can show you adequate examples about how to achieve this with custom code.

#1321151

Thank you for your super speedy reply!!!

I don't want anything too complicated and can understand your points!

I just require an incremental number to be automatically inserted and thought that using the cat ID would be the easiest way!

So, is it possible just to have an incremental number entered? And I may need to edit that number.

The purpose is to change the order of the categories displayed in a view, and I need to say, ok put this one before that one!

Eventually the number will not need to be edited, but for now I am inputting old entries and then sorting them into the correct order.

Or, if that is difficult and thinking on this, maybe a date would suffice? Could I have a field that enters the date the category is created and then I can change that date to force it into an order?

#1321159

The Term ID will be incremental but it's not ensured that it'll be "subsequent", means it may be 1, 3, 7 instead of 1,2,3,4,5,6,7.

You probably want a number that consecutively increments from 1 to n, augmenting by one each time you create a new term.

For this, wp_count_term() can help (it counts the terms in taxonomy), and PHP + operator to add 1 to that value, then wp_update_term_meta() to update the term meta field to store the new number.

This will always be the existing amount of terms +1 (hence a consecutively incrementing number).

This is also not possible natively, it is basically not possible at all to update a field in a post or term based on a value from somewhere else unless Types settings for the field or your own inputs.
That requires custom code in all cases.

To help you with such code I would need to know where and when this field should be saved and used later (in a View, I understand)

On the other hand, if you just want to order Terms, is there a reason you cannot create a View, and order the terms in that view by TERM ID?
This is possible in Views, if you query the Taxonomy of which you want to order the terms.
You then just select "order by" Term ID and output in the View loop any detail about the Term you need

Would that work?

#1321169

Normally, sorting by Term ID would work, and will certainly work for future posts. My problem is that I am inputting old data and I have not been given it in order!! So the Term IDs are incorrect for these

#1321171

I see.
But If the term id will not be correct, neither will the date or moment you input the term
That value, if calculated subsequently will be the same (at least relationally) to the Term ID:
Increasing by one as more you add, and that the other you add. This is the exact same as Term IDs will be

If you need a Custom field for some reason, then updating that with a valid depending on either Term or any other non-static value, requires custom code.

To help you with such code I need to know where and when this should happen

#1321173

[But If the term id will not be correct, neither will the date or moment you input the term
That value, if calculated subsequently will be the same (at least relationally) to the Term ID:
Increasing by one as more you add, and that the other you add. This is the exact same as Term IDs will be]

Yes, that is why I needed to edit the field afterwards. So if it is a date it would input the date of creation, but then if the post is out of order I can change the date so that it forces it into a position.

#1321177

OK, then we need a Custom Term Field with any possible value (I would suggest a Single Line) that you can update A) when creating the Term either with custom code or manually and B) later on, edit.
You can create a date field, if that suits your needs more, of course.

I cannot help with this unless I know where and when this use happen.
I can then show you some examples of code, and guide along a possible code solution, depending on where in the Website and when in the users workflow this should happen.

Thanks

#1321201

Thanks.

So, I would create a custom term date field for categories called createddate. This would fill in the date when a category is created. After saving the category I could manually edit this field to change the date.

[EDIT] Oh for categories already created previously, I need to be able to edit and add a date manually

Hope that makes sense!

#1321819

This makes sense, but it does not answer the main question, to which I need an answer to help you 🙂

Where should this happen?

See, "categories", or broader, taxonomy terms, can be created in the Taxonomy edit screen, when adding a post in the backend, or when adding a Post with a Toolset Form.

If this should happen where you edit taxonomies in the backend, then we could use create_term() hook, if it happens when saving a post (and after adding a taxonomy term to it) then we'd use save_post, and if it's in a Toolset Form, likely we'd use cred_save_data().
https://developer.wordpress.org/reference/hooks/create_term/
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Depending on where this updating of the field happens, we need to use another hook, and get the data in different ways.

#1322689

aahh, ok, I see now. hmm, never thought about that!

At the moment I am creating new categories in two ways: where you edit taxonomies AND when saving a post. If it's better to choose just one method, then I guess when saving a post would probably be best.

#1322793

I feared so 🙂

This requires a lot of custom code.

It requires a bit less if creating the terms only in the Taxonomy Edit Screen, but in the post edit, you'd have to hook into save_post, from there get the attached term, it's meta field and updates that with the date.

The only toolset related thing is that the Meta field will be with a prefix wpcf-, that's basically all. The rest will depend on WordPress and PHP, independently from Toolset altogether.

If you are not experienced with Custom code at this point I'd suggest contacting a contractor https://toolset.com/contractors/, because in the Support we cannot provide this complete.

I can give you the path to this, along with examples.

Let's assume we work on the save_post, hence when editing a post.

You'd use the save_post hook of WordPress:
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
This allows you to fire your Custom Code at the right moment, so you'd put your custom code in a custom function, and then fire that function with above hook on save_post:

function custom_function( $post_id ) {
    //All your custom code, like getting and updated terms and term fields
}
add_action( 'save_post', 'custom_function' );

I suggest studying the Codex above for a better understanding of the hook and how you can create a custom function for it.

Then, in/with that function we need to:
- determine if the post is new or edited (we do not want to work it when we edit the post...)
- get the term saved for the post,
- determine the date to use
- update that term field with the date

You can determine if the post is new or not within the save_post hook itself:
(this is related to the save_post hook, see here how to determine that: https://wordpress.stackexchange.com/questions/48678/check-for-update-vs-new-post-on-save-post-action/185991)

After this, you can concentrate on the "//All your custom code, like getting and updated terms and term fields" part.
There you can get the term saved for the post with code as described here: https://wordpress.stackexchange.com/questions/74017/get-terms-on-save-post-hook (Using $_POST[])
Example:

$term_addeed_to_post = $_POST['tax_input']['your_custom_tax']

You can now use the data from $_POST to get/update the Term Meta with the WordPress API get_term_meta() and update_term_meta():
https://codex.wordpress.org/Function_Reference/get_term_by
https://developer.wordpress.org/reference/functions/update_term_meta/
Example n(we use $term_addeed_to_post from previous code sample) :

update_term_meta( $term_addeed_to_post, 'wpcf-your-term-field', '$date_timestamp' );

Notice the $date_timestamp, this is the date you want to update for the field, it should be a Timestamp, this could be generated for example using date() PHP native function:
hidden link
You'd use the "U" format (Timestamp)

That's basically it, after above code, you'd close the function and when saving a new post it'd get the term added and update its meta with a timestamp.

Note that my code samples do not include safety checks like wether we deal with arrays, empty values or non existing values, this would require more custom work on it, but the outline should allow you to proceed (not without any PHP knowledge, but I am limited in what I can provide for support here in the forum, unfortunately).

Please let me know if something's not clear.

#1322909

And I thought it should be an easy thing to do LOL!

Thank you - I'll look into this and give it a whirl

#1322911

Please let me know if at any point you struggle, I can always try to help or give more insight.

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