Category not being assigned properly

This topic contains 8 reply and 3 voices, and was last updated by Alex 10 years, 11 months ago
Viewing 8 Posts - 1 through 8 (of 8 total)
Author Posts
May 23, 2013 at 10:09 pm 4095
Alex Hi, Frontend form for custom post type allowing users to select custom post type categories. Drop down list shows the categories correctly for that post type. However, the selected category does not save properly and a new one seems to be created with just a number. e.g. I selected category syncrude and 39 was created and saved with the post. Can you help please...
May 24, 2013 at 5:19 am 4099
Alex Alex

Hi ForestNation,

I am having the exact same issue with a custom taxonomy on a custom post type. I can include the custom taxonomy successfully on the form, but then when it save it converts all terms to a #number.

May 24, 2013 at 9:55 am 4105
Tareq Hasan Tareq Hasan

The problem happens when the taxonomy is not hierarchical, so making it hierarchical will solve the problem.

May 24, 2013 at 1:28 pm 4146
ForestNation ForestNation

Thanks Tareq, this worked.

May 24, 2013 at 3:07 pm 4150
Alex Alex

I want my custom taxonomy to act like tags, though, not hierarchical…

May 24, 2013 at 10:57 pm 4170
Tareq Hasan Tareq Hasan

Take a look here.

May 24, 2013 at 11:46 pm 4175
Alex Alex

Thanks for the reply, Tareq.

That is similar to what I’m looking for but not exactly the same. I want to have a non-hierarchical custom taxonomy and have them in checkboxes on the form, not text inputs. That way the customer has to check off which custom taxonomy terms apply.

I can add this to the form, no problem. But the problem arises when the terms are saved and get converted into numbers.

For now I have changed the custom taxonomy to be hierarchical as a way around this, but not sure if that will cause problems down the line.

May 25, 2013 at 10:17 am 4185
Tareq Hasan Tareq Hasan

So open up /class/frontend-form.php line 270, you’ll see this code wp_set_post_terms( $post_id, $_POST[$taxonomy['name']], $taxonomy['name'] );, replace this with the code below and should be working:
[php]
if ( is_taxonomy_hierarchical( $taxonomy[‘name’] ) ) {
wp_set_post_terms( $post_id, $_POST[$taxonomy[‘name’]], $taxonomy[‘name’] );
} else {
if ( $tax ) {
$non_hierarchical = array();

foreach ($tax as $value) {
$term = get_term_by( ‘id’, $value, $taxonomy[‘name’] );
if ( $term && !is_wp_error( $term ) ) {
$non_hierarchical[] = $term->name;
}
}

wp_set_post_terms( $post_id, $non_hierarchical, $taxonomy[‘name’] );
}
} // hierarchical
[/php]

May 25, 2013 at 9:41 pm 4206
Alex Alex

that seems to be working! thank you very much Tareq. we could mark this as resolved.

(for anyone else, the filename in question is /class/frontend-form-post.php…)

Viewing 8 Posts - 1 through 8 (of 8 total)