Support » Plugins » Filter categories on custom fields?

  • I’m writing a filter plugin that will filter the list of categories in the sidebar, to show only categories with posts that has a certain custom field. It’s my first plugin.

    I understand I can’t filter the output from the wp_list_categories() function in the template, since it’s mixed with HTML. But that function calls get_categories(), which has the following hook:

    $taxonomy = apply_filters( ‘get_categories_taxonomy’, ‘category’, $args );

    The ‘get_categories_taxonomy’ filter hook is undocumented. The docs mention a filter hook named ‘get_categories’, but it doesn’t exist in the code. And the get categories() function is supposed to return an array of category objects, but there is no Category class…

    So, in the absence of proper docs, I must ask for assistance. 🙂 How would you approach this task?

Viewing 5 replies - 1 through 5 (of 5 total)
  • You should find some filter docs (and the actual source code) here:

    Thanks. It helped so far as to tell me that ‘get_categories_taxonomy’ is new in 2.7. But the filter appears early in the get_catagories() function, so I’m not sure if I can use it. What I want is the output array from get_catagories().


    Two tips for digging into WordPress core:

    1) Google “wordpress functionname” and look for a phpxref site. Usually these cross-references will show which core file the function is in (then it’s a simple matter of opening the file up from your site) and all the calling files (to see how it’s used) and then a link to source code. (You seem to have already perused the get_categories() code)

    2) When writing code, if it’s hard to work out what WordPress is giving you in a function return (because there are many levels of functions, etc) add print_r($result); to your code and look in your browser for a dump of the array or object.

    So try

    $whoknows = get_categories();

    to see what you get. Hopefully it’s useful.

    Unfortunately the Taxonomy stuff is fairly complex.

    After looking into this for a related task (I thought get_categories_taxonomy would solve my problem) – get_categories_taxonomy appears to only be listed there so you can hook into it – I don’t think it is even called from anywhere else in the wordpress core. I’m not even sure what you’d be able to do with it.

    I’d be willing to bet that your solution (although this is 3 months old – hopefully you found one already..) lies in the get_terms() function, which is where the action really happens.

    Oops. Double post.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Filter categories on custom fields?’ is closed to new replies.