WordPress.org

Ready to get started?Download WordPress

Forums

Filter categories on custom fields? (6 posts)

  1. Southbound
    Member
    Posted 5 years ago #

    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?

  2. Roger Theriault
    Member
    Posted 5 years ago #

    You should find some filter docs (and the actual source code) here:
    http://adambrown.info/p/wp_hooks/version/2.7

  3. Southbound
    Member
    Posted 5 years ago #

    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().

  4. Roger Theriault
    Member
    Posted 5 years ago #

    @Southbound...

    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();
    print_r($whoknows);

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

    Unfortunately the Taxonomy stuff is fairly complex.

  5. Peter Butler
    Member
    Posted 5 years ago #

    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.

  6. Peter Butler
    Member
    Posted 5 years ago #

    Oops. Double post.

Topic Closed

This topic has been closed to new replies.

About this Topic