• amdh

    (@amdh)


    Hi

    My site has hierarchy categories with lot of subcategories. is it possible for my blog authors to select subcategory and then parent categories will be selected automatically?

    thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Joy

    (@joyously)

    Yes, this is actually preferred, so that the same content isn’t shown on multiple pages (for SEO). Although, if you only specify subcategories for posts, and then go to the parent category archive page, it will show all the posts in the subcategories.

    Moderator bcworkz

    (@bcworkz)

    I don’t think Joy is interpreting your question the same way I was. From her interpretation’s perspective, I’m sure she is 100% correct.

    My interpretation is you want a category term’s parent automatically assigned to the post as it is being saved. This is possible, but it’s kind of messy and requires custom coding. When a post is saved, the selected category term IDs are sent to the server. There’s no way to tell from this data alone if any particular ID has a parent or what it might be. So the code would need to get all the categories in existence and look up each selected ID to see if there is a parent and determine who that parent would be. And if you need the top level ancestor instead of the immediate parent (in the case of several hierarchy levels), it’s even more complicated. After all of that, it could turn out the parent was selected all along.

    As Joy points out, an archive list of the parent category will include the children whether the parent was assigned to the post or not. Maybe you have other reasons for requiring the parent be assigned, but appearance in parent archive lists would not be a good reason.

    Thread Starter amdh

    (@amdh)

    thanks for your replies.

    my problem is breadcrumb. when parent categories are not selected, the breadcrumb is incomplete. i use wp_get_post_terms() for showing breadcrumb because my category is a taxonomy.

    • This reply was modified 6 years ago by amdh.
    Moderator bcworkz

    (@bcworkz)

    I see. Auto-adding parents on post save like I explained would solve that, but it is rather messy. If your concern is solely the breadcrumb appearance and having the parent term be assigned to a post is not otherwise a concern for other purposes, I recommend altering the breadcrumb code to account for missing ancestors and not attempt to add such terms to the post itself. Primarily because wp_get_post_terms() can return complete term objects. There is more to work with here than the list of IDs we get when saving posts.

    For any given term object, you can build a hierarchical breadcrumb by getting that term’s ancestors with get_ancestors(). Then it does not matter if the parent term is assigned to a post or not. A post that has a parent term assigned actually complicates the process. Code would not know offhand which term to use for get_ancestors(). You could get ancestors for all terms that have a non-zero parent property and go with the one with the most ancestors. Not sure how to choose if there’s a tie. Perhaps it would not occur on your site.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘select hierarchy categories’ is closed to new replies.