Specifying a Preferred Category Hierarchy

As part of the best practises for organizing your WordPress site so that Breadcrumb NavXT’s output is what you expect, having a post be a member of multiple category hierarchies is something you should not do. While it still isn’t something you should do, there is a way to specify which category hierarchy is more important for a specific post.

The idea is that the order of the categories being returned by get_the_terms() can be modifed so that the more important category hierarchy is output first. This takes advantage of Breadcrumb NavXT’s behaviour of choosing the first category (term) hierarchy it finds for a post.

This is where Order Bender comes in. Order Bender gives you a nice post meta box to select the primary category for a post, and will modify the output order of get_the_terms() accordingly. Simple as that.

Installation is quite easy:

  1. Download the Master branch zip archive from GitHub
  2. In your WordPress Dashboard navigate to the plugin uploader (Plugins > Add New > Upload)
  3. Upload the zip archive
  4. Activate and enjoy!

You can keep up with day to day development via the Order Bender’s GitHub Repository.

-John Havlik

[end of transmission, stay tuned]

31 thoughts on “Specifying a Preferred Category Hierarchy

  1. Hi there,

    I have a couple of custom post types set up with different categories and would like to be able to use this plugin to specify which category to use for the breadcrumbs when I select multiple categories for a CPT.

    I installed the plugin, however the meta box doesn’t appear under my custom post types. Is there a way to modify it to work with my CPTs?

    Thanks!

    • The plugin was designed as a crutch for users that didn’t want to start using Custom Taxonomies and Custom Post Types like they should be. A post (single instance of any post type) should never be a member of multiple terms of a hierarchical taxonomy type. I guess I could add support for other post types, though it’s not as simple to do as some may think.

  2. Hey John,

    Thanks so much for taking a look at this for me! I started searching for a solution to this issue months ago but gave up, and just recently came across your post.

    I uploaded the new file and am seeing the meta box on my custom pots type, however it show the categories from my blog rather than the categories I have specified for that particular post type.

    Perhaps I should have explained how my site is set up… I’ve got a regular blog with regular posts and the a custom post type called “events”. Under the events CPT I have multiple categories such as Concert, Charity, Theater, etc.

    Basically what I was trying to do is to be able to choose what shows up under my breadcrumbs when I have multiple categories selected for this events custom post type.

    Thanks again!

    • Well then you’re not really taking about categories (as in the specific taxonomy type of categories that is built into WordPress). You are already using a custom taxonomy. Order bender doesn’t support custom taxonomies right now (when thinking about supporting custom taxonomies I came up with several non-trivial scenarios/problems that I need to resolve).

      -John Havlik

        • I meant to say that I modified it to display my taxonomies, not post categories. I had my mind elsewhere. Feel free to delete any of my posts!

          • Ok, at first I was wondering what you were talking about. What I’ll probably do in the future (will get Order Bender into the WP.org plugin repo) is have it use the post taxonomy/hierarchy specified in the Breadcrumb NavXT settings page.

            -John Havlik

  3. Hi John, I’m looking for the exact same thing as Teena. I installed the plugin but it’s only showing regular post categories. My issue with Breaadcrumb NavXT is that it is choosing the taxonomy based on alphabetical order instead of ID. Aside from renaming the taxonomies (which wouldn’t make much sense), I can’t find a way to change the way this is working. “Accommodations” is always chosen over “Things to Do” for instance. Sorry if this is in the wrong spot. I also created a WP forum thread Here

    • Breadcrumb NavXT doesn’t choose by alphabetical order, it chooses the first returned taxonomy term (if the taxonomy is hierarchical then it picks the first term that is a child of another term). Usually this is by ID, but you may have other plugins modifying the order for some reason.

      -John Havlik

  4. Hei John,

    I installed the plugin and started working fine for some categories, but after rechecking everything I realized that it’s not working with a couple of categories (“Gente” & ” rutas”).

    Do you have any idea how could I fix that, or if there’s anything I’m doing wrong?

    Thanks in advanced ;)

    • Franc,

      Can you explain more about what is happening? Is the post a member of multiple categories and is one of the other ones a child of another category? This may be a result of how Breadcrumb NavXT picks taxonomy terms (first found hierarchy). If you don’t have a hierarchy (e.g. if “Gente” and “rutas” are not child categories), this plugin isn’t going to be of much help.

      -John Havlik

  5. Hi John,

    Thanks for putting this together, works very well. However, it only seems to display a limited number of categories in the meta box, rather than all the categories. Is there a limit on this? It doesn’t seem to be just top level categories, so it seems odd to cut off at a certain number.

    Cheers,
    Mun Yin

    • Order Bender was created specifically to exploit the behavior of Breadcrumb NavXT to achieve this result. It may work with other breadcrumb plugins (depends on how the breadcrumb plugin chooses which category to place in the trail). However, I don’t follow the other breadcrumb plugins very closely, and did not test this against any other breadcrumb plugin. I have been developing/supporting/maintaining Breadcrumb NavXT (far) longer than any other currently active breadcrumb plugin has existed.

      -John Havlik

  6. I have a problem. When I choose categories with parent category and sub category and I chose 1 primary category. Your pluggin can’t show right. So I check your code and I add my code:
    function reorder_terms($terms, $post_id, $taxonomy)
    {

    //Get the prefered taxonomy term for the post here
    $pref_id = get_post_meta($post_id, $this->unique_prefix . ‘_’ . $taxonomy . ‘_prefered’, true);
    //Make sure that ID is in the array
    foreach($terms as $k => $v){
    if($v->parent == $pref_id){
    //Store our prefered term
    $perf_term = array($pref_id => $terms[$k]);
    //Remove it from the array
    unset($terms[$pref_id]);
    //Recombine the array
    $terms = $perf_term + $terms;
    break;
    }
    }

    if(array_key_exists($pref_id, $terms))
    {
    //Store our prefered term
    $perf_term = array($pref_id => $terms[$pref_id]);
    //Remove it from the array
    unset($terms[$pref_id]);
    //Recombine the array
    $terms = $perf_term + $terms;
    }
    //
    //Return the array
    return $terms;
    }

  7. Hi, is it possible to select a preferred term for a flat taxonomy (ie non-hierarchical) like the default tags? When I install, the Order bender meta boxes only show up for the hierarchical taxonomies. Thanks!

    • Chris,

      Breadcrumb NavXT, by default, will display all member terms if the hierarchy for the post is set to a taxonomy that is non-hierarchical (thus having non-hierarchical taxonomy terms in Order Bender does not make sense).

      -John Havlik

      • Hi John,
        Thank you for your helpful reply and for Order Bender, which is one of the most useful and clever WordPress plugins I have used. In fact the solution is much simpler than I thought – it just took me some time to realize it. I simply set the taxonomy argument ‘hierarchical’ => true and use my taxonomy as a non-hierarchical taxonomy. Unless you need your site’s contributors to be able to create new terms it works great. You as admin will only create parent terms in your taxonomy and it will function as a non-hierarchical taxonomy with the possibilty to select a preferred term with Order Bender.

  8. This is a great little plugin, thank you.

    I am just wondering if it would be easy to make it compatible with the yoast breadcrumbs tool.

    As far as I know, YOAST returns “what wordpress returns” in its breadcrumb structure.

    With your plugin I can prioritize the category order in the post metas, but not in the YOAST breadcrumbs.

    Thanks again for this.

      • Hi John, thanks for the quick reply.

        I didn’t mean to apply the hierarchy of a WP menu.

        I meant apply the Order Bender Hierarchy to the YOAST breadcrumbs, and not only to the way the post categories are shown.

        I might give NavXT a try anyway. Thanks!!

        • Hi Francesco,

          After re-reading you previous comment, it appears I definitely didn’t read it closely enough the first time. Sorry about that.

          I haven’t looked at Yoast’s breadcrumbs in a while (how they’re implementing them). Order Bender was created to exploit a behavior in how Breadcrumb NavXT picks a term (e.g. category) hierarchy. That is, Breadcrumb NavXT picks the first term with a parent it finds for a post, or the last term if no terms have a parent. If Yoast uses a similar method, then Order Bender will work with it. Though, it appears this isn’t the case.

          As a semi-related aside, the current implementation, while it will still work, is going to be obsoleted by a filter being made available in Breadcrumb NavXT 5.4.0. This allows better control over the decision. Currently Order Bender can not get Breadcrumb NavXT to pick a term that does not have a parent if there is a term with a parent. The filter provides a method to do that.

          -John Havlik

  9. Perfect! Just what I was looking for. Other plugins promised the same, but did not perform as promised in conjunction with Nav XT.

    Thank you!

Comments are closed.