Should I register both parent and child as 'hierarchical' => true
, if there's no grandchild?
If "Movie" is the top level taxonomy, when I register a child, is it as simply as: 'parent_item' => __( 'Parent Movie' )
?
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');
If I query a parent taxonomy, will the child be included in the results or not?
If the taxonomies are not listed in the args
of register_post_type
, will they still work?
Should I register both parent and child as 'hierarchical' => true
, if there's no grandchild?
If "Movie" is the top level taxonomy, when I register a child, is it as simply as: 'parent_item' => __( 'Parent Movie' )
?
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');
If I query a parent taxonomy, will the child be included in the results or not?
If the taxonomies are not listed in the args
of register_post_type
, will they still work?
Too much for a comment, so I'll leave it for an answer:
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.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.save_post
and similar. You'll find enough answers here on WPSE that show you how. You're close.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:
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');