Skip Navigation

[Resolved] Use Custom Select List To Save Post Category With CRED Form

This thread is resolved. Here is a description of the problem and solution.

Problem: Instead of the standard CRED select field, I would like to use a custom select field in my CRED form to choose a taxonomy term I wish to associate with this post.

Solution: Use the following template to create your select element:

<select name="category">
      <option>- Select a model -</option>
        <wpv-loop>
            <option value="[wpv-taxonomy-id]">[wpv-taxonomy-title]</option>
        </wpv-loop>
    </select>

Then update your functions.php file to capture and store the selected term along with the post:

//Create a dynamic category by the CRED form.
add_action('cred_save_data','func_save_cat_id_for_cred_post',10,2);
function func_save_cat_id_for_cred_post($post_id,$form_data) {
    if ($form_data['id']==6814) {
        wp_set_post_terms( $post_id, $_POST['category'], 'category', false );
    }
  }

Relevant Documentation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_set_post_terms

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

Last updated by PhantomPWR 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#559201

I am trying to:

Use my own select dropdown(via a view inside the form) for selecting a category in the CRED post submission form.

I use a URL parameter (terms-filter=xyz), to filter only the categories I want in my select dropdown. The default CRED category select dropdown lists ALL the categories, which is not useful in my case.

Link to a page where the issue can be seen:

hidden link

I expected to see:

The submitted post in my selected category in WP Admin -> Posts

Instead, I got:

The submitted post is saved under "Uncategorized"

#559346
Screen Shot 2017-08-13 at 1.15.03 PM.png

Hi, you have a couple of JavaScript errors thrown on this page, and I'm not really able to troubleshoot effectively until those are resolved. Can you take a look? I've attached a screenshot here.

I use a URL parameter (terms-filter=xyz), to filter only the categories I want in my select dropdown.
Be careful here. You've got terms-filter used as a filter parameter in the category-parent View, but terms_filter used to define a default value in your CRED field shortcode.

[cred_field field='category' display='select' single_select='true' output='bootstrap' value='Select model' urlparam='terms_filter']

Looks like trouble to me, I would consider consolidating or using very different parameter names instead of two similar names.

//Create a dynamic category by the CRED form.
/*
add_action('cred_save_data','func_cat_id',10,2);
function func_cat_id($cat_id,$form_data) {
    if ($form_data['id']==6814) {
        $args = array('term_id' => $cat_id);
        wp_update_post($args);
    }
  }
*/

I'd like to point out 2 issues I noticed right away.

First, cat_id isn't going to be help you access a category term ID. It's going to be the ID of the post created by CRED. Please review the documentation for the cred_save_data hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
The first parameter will always be the post_id, and you can't just change the variable name to get something else here.

Second, I think you should use wp_set_post_terms to manage terms, not wp_update_post:
https://codex.wordpress.org/Function_Reference/wp_set_post_terms
You will have access to the post ID in the first callback function argument, and you can get other form selections from $_POST.

I'll be glad to go into more detail here, but I would need some more information from you about how you expect this to work. I'm not clear what you're trying to accomplish with the category parent and category select fields.

#559355

Hi Christian,

Thanks for getting back to me.

1. Javascript errors - sorted
2. urlparam="terms_filter" - changed to urlparam="terms-filter"

Just a quick explanation of what I'm trying to achieve - I'll use the current form, categories, etc as an example:

1. For this particular form, the category hierarchy is: Commercial(Aircraft Type) -> Airbus(Aircraft Manufacturer) -> A300-600(Aircraft Model), A300B4-200(Aircraft Model), A300B4-203(Aircraft Model), etc.

2. The "terms-filter=14" parameter in the URL, is the category id of the aircraft manufacturer(Airbus)

At the moment, the CRED category select dropdown, displays ALL available categories on the site. What I need, is to display the list of child categories(models) of the direct category parent(manufacturer: Airbus, id: 14).

When you click on "COMMERCIAL" in the menu, you'll see the list of manufacturers with their respective ids.

At the moment, you'll see two select dropdowns in the form. Both of them work halfway.

The first one is my own, which is generated by a parent/child view, which displays the correct categories(models), depending on the "terms-filter" URL parameter. Although the display is correct, the post is saved under "uncategorized".

The second dropdown select is the default CRED category dropdown, which saves the post in the correct category, but doesn't display the correct categories.

So, I either need a way for my selected category to be "recognised" by the form OR I need to be able to tell the CRED dropdown to only show the relevant (child)categories.

Does this make sense?

I'd really appreciate your help and/or advice! ????

#559390

Okay I think it's working correctly now, please confirm. I submitted several tests and set them to "Draft" status - feel free to delete them. Here is what I did:
- Modified your functions.php file:

//Create a dynamic category by the CRED form.
add_action('cred_save_data','func_save_cat_id_for_cred_post',10,2);
function func_save_cat_id_for_cred_post($post_id,$form_data) {
    if ($form_data['id']==6814) {
        wp_set_post_terms( $post_id, $_POST['category'], 'category', false );
    }
  }

If you store local copies of this file, be sure to download this update.

- Deleted the 2nd, standard CRED category field

- Modified the child View's Loop Output to use name="category" on the select field:

<select id="d1cc" class="model-select-list" name="category" data-wpt-type="select">
      <option>- Select a model -</option>
		<wpv-loop>
			<option value="[wpv-taxonomy-id]" data-wpt-name="category" data-wpt-type="option">[wpv-taxonomy-title]</option>
		</wpv-loop>
	</select>
#559433

Hi Christian,

You are a hero!

I can confirm that the form now works perfectly.

Thanks a million and have an awesome day! 🙂

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