Skip Navigation

[Resolved] post-category form custom field

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I have a Form that edits posts, which includes a custom field. I would like to automatically set a taxonomy term on the post when the Form is submitted. The term should be determined by the value of the custom field.

Solution: Use the function wp_set_object_terms to set a term on a post:

wp_set_object_terms($post_id, 'term-slug', 'category', false);

Instead of term-slug you can use:
A single term slug, single term id, or array of either term slugs or ids. This will replace all existing related terms in this taxonomy. Passing an empty value will remove all related terms.

The last parameter false will delete all the other category terms from this post. If you use true instead of false, the existing category terms will not be deleted.

Relevant Documentation:
https://developer.wordpress.org/reference/functions/wp_set_object_terms/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This topic contains 2 Antworten, has 2 Stimmen.

Last updated by martinB-16 vor 1 Monat, 2 Wochen.

Assigned support staff: Christian Cox.

Author
Artikel
#1723189

I want to set the category of a post using a custom field.
I tried the following code, but it doesn't work. I suspect that '' wpv-post-category "is wrong.

"wpcf-beitrag_typ" is the custom field in the form

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_action('cred_save_data','func_update_kategorie',10,2);
function func_update_kategorie($post_id,$form_data) {

if ($form_data['id']==751) {

$kategorie = get_post_meta($post_id, 'wpcf-beitrag_typ', true);

update_post_meta($post_id, 'wpv-post-category', $kategorie);
}
}

#1723229

Christian Cox
Supporter

Languages: Englisch (English )

Timezone: America/New_York (GMT-04:00)

Hello, if you want to add a taxonomy term to the post, you should use the function wp_set_object_terms: https://developer.wordpress.org/reference/functions/wp_set_object_terms/

The update_post_meta function is used to set a custom field value. Taxonomies are not the same as custom fields, so the wp_set_object_term function is better. Example:

wp_set_object_terms($post_id, 'term-slug', 'category', false);

Instead of term-slug you can use:
A single term slug, single term id, or array of either term slugs or ids. Will replace all existing related terms in this taxonomy. Passing an empty value will remove all related terms.

The last parameter false will delete all the other category terms from this post. If you use true instead of false, the existing category terms will not be deleted.

Let me know if you have questions about this.

#1728749

My issue is resolved now. Thank you!

Das Forum „Toolset professionelle Unterstützung“ ist für neue Themen und Antworten geschlossen.