Skip Navigation

[Geschlossen] When child term is set, automatically set parent term of that child

This support ticket is created vor 10 Jahre, 8 Monate. 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.

This topic contains 9 Antworten, has 2 Stimmen.

Last updated by Matthew vor 10 Jahre, 8 Monate.

Author
Artikel
#10903

Consider a 'location' taxonomy with hierarchical terms like so:
Country
-Province/State
--City

If a post is given a city term of 'Denver', then the parent terms of 'Colorado', and 'USA' should automatically be assigned to the post.
I expect this will someday be default option in wordpress but I'm hoping someone can help me with a solution now.

I have tried the plugin "parent category toggler" but that only works when the terms are set from wordpress backend. I need the association to be inherited automatically because my taxonomy terms will be saved/edited on the website's front end using a variety of methods.

The following hook uses a different method from the above plugin, but has the same problem of only working from the backend.

add_action('save_post', 'jr_select_parent_terms', 10, 2); // automatically select parent terms
function jr_select_parent_terms($post_id, $post) {
if( $post->post_type == 'revision' ) return;
$taxonomies = get_taxonomies(array('_builtin' => false));
foreach ($taxonomies as $taxonomy ) {
$terms = wp_get_object_terms($post->ID, $taxonomy);
foreach ($terms as $term) {
$parenttags = get_ancestors($term->term_id,$taxonomy);
wp_set_object_terms( $post->ID, $parenttags, $taxonomy, true );
}}}

Any ideas would be greatly appreciated.

Regards,
Matt

#10942

Hi Matthew,

The 'save_post' action is what I would use for this. Are you sure it's not being called. It should work on the frontend or backend when wp_insert_post is called.

What are you using to save your posts?

Best regards,
Bruce.

#10952

Hi Bruce,
I'm using gravity forms for post creation and frontend editor for editing of post fields and taxonomy terms.

Using the code above, 'save_post' effectively sets the appropriate parent term upon updating the post from the backend only. When a post is created through gravity forms or edited with frontend editor, nothing happens. I assume that means that those plugins don't call the 'save_post' action?

I'm a total beginner php and so I'm really struggling with this. Your thoughts and advice are met with seeeeerious gratitude. Would you want wordpress/ftp access to my site?

Cheers,
Matt

#10963

Hi Mathhew,

Where do you have your code inserted. Maybe it's only being called in admin mode. Check for something like if(is_admin())

Best regards,
Bruce

#10969

Its in theme's function.php, no is_admin anywhere. I've tried throwing it in the plugin folder but it still only worked in the backend.

#10972

Hi Matthew,

Gravity Forms has hooks for form submission. You might have to use those.

Best regards,
Bruce.

#10981

Ya, I'm working on the gravity forms hook - but I'd much prefer this behavior to hook into the core of wordpress to make this behavior default.

I may be close to a solution but I'm not sure:

The 'jr_select_parent_terms' function from above is effective if I use it in my page templates and call it from within the loop. However, does that mean it will be unnecessarily looping through arrays, even if the post has not been edited? If so, do you think that would noticeably affect my loading time as the number of posts increases?

#10982

Hi Matthew,

I couldn't really say if it's going to affect your loading time. I guess you're adding a little extra time for each post in the loop.

Best regards,
Bruce.

#11007

Since this is outside the scope of wp-types, I'll close the this thread.

As a temporary measure I'm using the function described above from within the loop, works fine on a small scale site but I may bloat the site as posts are added.

I'll continue to look for better solutions and will post here if I find one.

I've submitted this topic to wordpress as an idea. If you're interested - please upvote it here: http://wordpress.org/extend/ideas/topic/if-child-taxonomy-term-is-selected-automatically-select-set-the-parent-term

Thanks for your help Bruce

#11016

The hook filter below works. My troubles above were mostly caused because I was hooking into the "save_post" action which does not affect my custom post types. I needed to substitue 'post' with my post type slug.

Note: that I did not create my post types with wp-types for the reason that they do not register custom_post capabilities. Not sure if this is connected to the action or not.

add_action('publish_YOUR-POST-TYPE', 'select_parent_terms', 10, 2); // automatically select parent terms
function select_parent_terms($post_id, $post) {
if(!wp_is_post_revision($post_ID)) {
$taxonomies = get_taxonomies(array('_builtin' => false));
foreach ($taxonomies as $taxonomy ) {
$terms = wp_get_object_terms($post->ID, $taxonomy);
foreach ($terms as $term) {
$parenttags = get_ancestors($term->term_id,$taxonomy);
wp_set_object_terms( $post->ID, $parenttags, $taxonomy, true );
}}}}

Das Thema „[Geschlossen] When child term is set, automatically set parent term of that child“ ist für neue Antworten geschlossen.