custom taxonomy - How to register child taxonomies?

admin2025-01-07  5

  1. Should I register both parent and child as 'hierarchical' => true, if there's no grandchild?

  2. If "Movie" is the top level taxonomy, when I register a child, is it as simply as: 'parent_item' => __( 'Parent Movie' )?

  3. How to automatically asign a child taxonomy to a post? So far, I figured out how to asign a taxonomy to a post, but, does this work fine with child taxonomy? wp_set_object_terms($post_ID, $cat, 'category');

  4. If I query a parent taxonomy, will the child be included in the results or not?

  5. If the taxonomies are not listed in the args of register_post_type, will they still work?

  1. Should I register both parent and child as 'hierarchical' => true, if there's no grandchild?

  2. If "Movie" is the top level taxonomy, when I register a child, is it as simply as: 'parent_item' => __( 'Parent Movie' )?

  3. How to automatically asign a child taxonomy to a post? So far, I figured out how to asign a taxonomy to a post, but, does this work fine with child taxonomy? wp_set_object_terms($post_ID, $cat, 'category');

  4. If I query a parent taxonomy, will the child be included in the results or not?

  5. If the taxonomies are not listed in the args of register_post_type, will they still work?

Share Improve this question edited Oct 19, 2011 at 16:14 kaiser 50.8k27 gold badges150 silver badges244 bronze badges asked Oct 19, 2011 at 14:11 JennyJenny 1,7674 gold badges26 silver badges41 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

Too much for a comment, so I'll leave it for an answer:

  1. You're asking a lot of Qs that should actually be different Qs and not dumped into a single one.
  2. (Ad 1.) You don't register "child" or "parent" taxonomies. You add (child-)terms to a hierarchical taxonomy via the WP Back-End UI. You can do it programmatically with wp_set_post_terms();, wp_set_object_terms();, or other functions, but that's more internal stuff and tough to handle as you'd need to check on every request if it already exists or not (adds DB-queries), so it's not really recommended as you can't do it on theme activation as long as WP doesn't offer a hook there.
  3. (Ad 4.) You can query multiple taxonomies at one time as seen on the WP_Query article in Codex. This queries the taxonomy and all assigned posts. You can also query for terms that are inside a hierarchical taxonomy. But there are no "child" taxonomies. Only "child" terms. You can add those child terms to your tax_query arguments. Doing something like if is child of can be done with functions like get_term_children();. If you link to a term archive, you can modify the query (search WPSE) to include child terms as well.
  4. (Ad 3.) As stated in other answers & comments to some of your other Qs, you'll need to hook in stuff like save_post and similar. You'll find enough answers here on WPSE that show you how. You're close.
  5. (Ad 2.) You're mixing labels with actual arguments. The parent_item and parent_item_colon arguments are meant to set your custom text and can't register "child" taxonomies. They're only used to modify the default text you see in the admin UI.

Sidenotes:

  • Please care about your old Qs. We really got problems to lower the number of open & unanswered Qs. You should go back and try to work on your open Qs that have answers.
  • Proper formatting is highly appreciated. Answerers invest their time & advice for free to help you solve your problem. Please put an equal amount of effort into asking and formating a Q.
  • Be polite in your comments to an answer. This will a) motivate the answerer to offer further help and b) this is an open Q/A format and your Q is meant to help other users later to avoid asking the same Q again and instead just read your Q and the according answers. See your behavior when asking & commenting as something like your "buisness card".

If you register a taxonomy as hierarchical=>true, it will become a category, and you can form parent - child relationships within that taxonomy. Whereas if you register a taxonomy as hierarchical=>false, it will become a tag, meaning, no parent - child relationships. You may need to get a term's id, use the function term_exists, then use wp_insert_term to insert a new term, with parent as the id you got using term_exists.

function create_child_taxonomy() {
    // Register a child taxonomy (e.g., 'subgenre' under 'genre')
    $args = array(
        'hierarchical' => true,
        'labels' => array(
            'name'              => 'Subgenres',
            'singular_name'     => 'Subgenre',
            'search_items'      => 'Search Subgenres',
            'all_items'         => 'All Subgenres',
            'edit_item'         => 'Edit Subgenre',
            'update_item'       => 'Update Subgenre',
            'add_new_item'      => 'Add New Subgenre',
            'new_item_name'     => 'New Subgenre Name',
            'menu_name'         => 'Subgenres',
        ),
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'subgenre'),
    );

    register_taxonomy('subgenre', 'post', $args); // Register 'subgenre' for posts
}
add_action('init', 'create_child_taxonomy');
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256684a381.html

最新回复(0)