0

I've got terms defined under a custom taxonomy. I want the terms to be pseudo-hierarchical, in that I want each term to be able to define a "parent" term that can be selected from a drop-down, but I do not want to use the other terms as the options (which is the default behavior). Instead of using the same list of terms as potential parents, I want to use a custom data source.

I see in the wp-admin/edit-tag-form.php file, starting on line 165, the following code that generates the drop-down list of parent terms:

$dropdown_args = apply_filters(
    'taxonomy_parent_dropdown_args',
    $dropdown_args,
    $taxonomy,
    'edit'
);
wp_dropdown_categories($dropdown_args);

And based on the documentation for wp_dropdown_categories, it looks like I can set up a hook into get_terms and then, theoretically, make use of WP_Term_Query to write my own custom SQL. (Although based on that code, I'm thinking I actually need to hook into taxonomy_parent_dropdown_args instead.) In any case, my question is: how do I do this?

I'm still relatively new to plugin development, and I haven't found any concrete examples of someone overriding the resultset of get_terms by using the WP_Term_Query class to define a custom query. I'm actually thinking about using an entirely new table, created by my plugin.

For example, suppose I wanted to query something like:

SELECT id, name FROM custom_parent_table WHERE taxonomy = %s

How would I go about implementing this with WP_Term_Query? (Or, if I'm off base and haven't got the right understanding of how these hooks and classes interact, how would I do it regardless?)

  • Maybe you can use a walker but I'm not sure if you would be able to change the order at that point. – Howdy_McGee Sep 28 '17 at 19:30
  • Just a thought, but could it be that it would make more sense to work with a non-hierarchical taxonomy. Then add a form field and work with term meta data. Instead of playing around with the parent drop down. Kind of like shown here, coincidently by @Howdy_McGee. – Nicolai Sep 30 '17 at 16:23
  • @Nicolai - that's a really good idea. The more I think about this, the more I think I was trying to over-complicate the solution. Adding a new field seems like the better way to go. – soapergem Oct 3 '17 at 14:48
  • @SoaperGEM My pleasure. In the end it really only depends on what you need for your use case. That I can't know of course, which is why just offered another idea. – Nicolai Oct 6 '17 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.