Support » Plugins and Hacks » [Resolved] Custom taxonomy URLs

[Resolved] Custom taxonomy URLs

  • I’m having some trouble with custom taxonomies. Here’s the code I use to register them:

    register_taxonomy('problem_type', 'attachment', array(
        'hierarchical' => false,
        'label' => __('Problem Type'),
        'query_var' => 'problem_type',
        'rewrite' => array('slug' => 'problem-type'),
    register_taxonomy('problem_difficulty', 'attachment', array(
        'hierarchical' => false,
        'label' => __('Problem Difficulty'),
        'query_var' => 'problem_difficulty',
        'rewrite' => array('slug' => 'problem-difficulty'),

    Then I use this code to print a list of them so I could browse through matching objects:

    <?php wp_tag_cloud(array('taxonomy' => 'problem_type', 'format' => 'list')); ?>
        <?php wp_tag_cloud(array('taxonomy' => 'problem_difficulty', 'format' => 'list')); ?>

    I do get a list, but the link is something like “http://www.sitename.org/?problem_type=foo” instead of the nice URL I expected like “http://www.sitename.org/problem-type/foo/”. Also the URL doesn’t work at all. It just my site’s homepage; the posts aren’t even filtered or anything.

    a) How do I get URL rewriting working with custom taxonomies?
    b) How do I actually get a list of matching objects?
    c) Is it OK that I’m using them for attachments or is that broken?

    Now I realize it’s a bit unusual that I’m using taxonomies on attachments, but I tried to use them on posts just to test out and had the identical problem.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Holy crap, I posted this question less than an hour ago and it’s already in Google.

    OK, I can add a little more information. I’m calling register_taxonomy when my plugin first loads. This seems to cause some trouble. Moving it to later in the request (I moved it to the widgets_init action to try because my plugin happens to already have a hook for that), and it seems to be better behaved.

    Also, it seems that $wp_rewrite->flush_rules() needs to be called, but only once, and not every request. I don’t know the best way to do that. E.g. once I release a new version of my plugin, and somebody upgrades, I want it to be called, but also if somebody installs the plugin for the first time…

    So, now I’ve got good looking URLs, and the cloud seems to draw correctly, but going there gives me a 404 instead of a list of matching objects…

    OK, I’ve figured this out.

    The confusion was caused because I had attached some of these terms to unpublished content. Currently, there is a limitation in the custom taxonomy system where get_terms always produces counts for terms that include unpublished objects. So wp_tag_cloud will make links to terms that have no published content, which is what causes the 404.

    As a workaround I created a filter on get_terms that does its own query to produce counts that only include published objects (but only when a special argument is given so that it doesn’t mess up things like admin screens).

    Hi. Do you mind posting the code for the filter you wrote?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Custom taxonomy URLs’ is closed to new replies.