A talk by @thorstenfrommen
In WordPress, a taxonomy is a grouping mechanism for some posts.
The names for the different groupings in a taxonomy are called terms.
WordPress Codex
register_taxonomy( $taxonomy, $object_type, $args );
if ( is_tax( $taxonomy, $term ) ) { ... }
if ( is_object_in_taxonomy( $object_type, $taxonomy ) ) { ... }
if ( has_term( $term, $taxonomy, $post ) ) { ... }
wp_set_object_terms( $object_id, $terms, $taxonomy );
$terms = get_terms( $taxonomy, $args );
$objects = get_objects_in_term( $term, $taxonomy, $args );
$posts = get_posts( array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'wordcamp-berlin' ),
),
array(
'taxonomy' => 'tag',
'field' => 'slug',
'terms' => array( 'wcber' ),
),
),
) );
add_action( 'registered_taxonomy', function ( $taxonomy, $object_type ) {
// ...
}, 10, 2 );
add_action( 'created_term', function ( $term_id, $tt_id, $taxonomy ) {
// ...
}, 10, 3 );
add_filter( 'get_term', function ( $term, $taxonomy ) {
// ...
}, 10, 2 );
register_taxonomy( $taxonomy, 'user', array(
// other arguments...
'rewrite' => false,
'capabilities' => array(
'manage_terms' => 'edit_users',
'edit_terms' => 'edit_users',
'delete_terms' => 'edit_users',
'assign_terms' => 'read',
),
// other arguments...
// other arguments...
'options' => array(
'cardinality' => 3,
'allow_empty' => true,
'tags' => true,
'priorities' => true,
),
// other arguments...
) );
created_term
;
edit_terms
;
edited_term_taxonomy
;
delete_term
.
Metadata, by its very definition, is information about information. In the case of WordPress, it's information associated with content in the comments, posts, or users table.
WordPress Plugin Handbook
_edit_lock
;
_menu_item_object_id
;
_wp_page_template
;
admin_color
;
first_name
;
meta-box-order_post
;
$admin_color = get_user_meta( $user_id, 'admin_color', true );
$related_posts = get_post_meta( $post_id, 'related_post' );
update_user_meta( $user_id, 'show_admin_bar_front', false );
delete_post_meta( $post_id, 'deprecated' );
$full_width_pages = get_posts( array(
'meta_key' => '_wp_page_template',
'meta_value' => 'templates/full-width.php',
) );
$jane_does = get_users( array(
'meta_query' => array(
array(
'key' => 'first_name',
'value' => 'Jane',
),
array(
'key' => 'last_name',
'value' => 'Doe',
),
),
) );
meta_value
column;
WP_Tax_Query
instead of WP_Meta_Query
;
$popular_posts = get_posts( array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'meta',
'field' => 'slug',
'terms' => array( 'popular-post' ),
),
),
'orderby' => 'rand',
'posts_per_page' => 10,
) );
$w_org_users = get_users( array(
'number' => -1,
'tax_query' => array(
array(
'taxonomy' => 'user_meta',
'field' => 'slug',
'terms' => array( 'has_w_org_account' ),
),
),
) );
if ( $w_org_users ) : ?>
<ul>
<?php foreach ( $w_org_users as $user ) : ?>
<li><?php echo get_user_meta( $user->ID, 'w_org_user', true ); ?></li>
<?php endforeach; ?>
</ul>
<?php endif;
if ( has_meta_term( $object_id, $meta_term_id_or_slug ) ) {
echo 'Yatta!';
}
WP_Term
Class
stdClass
objects.
$term = WP_Term::get_instance( $term_id );
if ( $term && 'wordcamp' === $term->taxonomy ) {
echo $term->name;
}
$new_term = new WP_Term( (object) array(
'name' => 'New term in the wild',
) );
Not to be confused with meta terms. (ツ)
{$table_prefix}_termmeta
table;
*_term_meta()
functions;
meta_query
parameters for term getters.