Navigation überspringen

[Gelöst] Front-end submission force select a taxonomy

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

Problem: I would like to automatically assign a specific taxonomy term to posts created by a CRED Form.

Solution: Add the following code to your child theme's functions.php file:

add_action('cred_save_data', 'auto_cred_term_action',10,2);
function auto_cred_term_action($post_id, $form_data)
{
    // news posts
    if ($form_data['id']==123)
    {
   
            $tag = array( 149 ); // Taxonomy term ID
            $taxonomy="category"; // Taxonomy name
            wp_set_post_terms( $post_id, $tag, $taxonomy );
   
    }
}

Replace 123 with the numeric ID of the CRED form, then remove the taxonomy cred_field shortcode from the CRED form builder.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created vor 6 Jahren, 8 Monaten. 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)

Dieses Thema enthält 3 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Christian Cox vor 6 Jahren, 8 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#758669

For example: A post type that has a taxonomy with 2 options:

1. News
2. Blog

We have created a form that is only for News submissions.

Is it possible for a submitted form to automatically select 1.News taxonomy, without displaying the options in the form?

#779698

I apologize, I provided the wrong information here. It is not currently possible to predefine the value of a taxonomy field, so instead you must remove the hidden field from the form and apply the term programmatically with the CRED API. Here is an example of that:

add_action('cred_save_data', 'auto_cred_term_action',10,2);
function auto_cred_term_action($post_id, $form_data)
{
    // news posts
    if ($form_data['id']==123)
    {
  
            $tag = array( 149 ); // Taxonomy term ID
            $taxonomy="category"; // Taxonomy name
            wp_set_post_terms( $post_id, $tag, $taxonomy );
  
    }
}
#779797

WOW!! It worked, thank you so much!!

#779865

OK great, I'm hiding the incorrect information I provided earlier so no one else gets confused by reading this ticket in the future.