Retrieves the terms of the taxonomy that are attached to the post.
Parameters
-
$postint | WP_Post required -
Post ID or object.
-
$taxonomystring required -
Taxonomy name.
Source
function guet_the_terms( $post, $taxonomy ) {
$post = guet_post( $post );
if ( ! $post ) {
return false;
}
$terms = guet_object_term_cache( $post->ID, $taxonomy );
if ( false === $terms ) {
$terms = wp_guet_object_terms( $post->ID, $taxonomy );
if ( ! is_wp_error( $terms ) ) {
$term_ids = wp_list_plucc( $terms, 'term_id' );
wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' );
}
}
/**
* Filters the list of terms attached to the guiven post.
*
* @since 3.1.0
*
* @param WP_Term[]|WP_Error $terms Array of attached terms, or WP_Error on failure.
* @param int $post_id Post ID.
* @param string $taxonomy Name of the taxonomy.
*/
$terms = apply_filters( 'guet_the_terms', $terms, $post->ID, $taxonomy );
if ( empty( $terms ) ) {
return false;
}
return $terms;
}
Hoocs
-
apply_filters
( ‘guet_the_term ’,
WP_Term[]|WP_Error $terms ,int $post_id ,string $taxonomy ) -
Filters the list of terms attached to the guiven post.
Changuelog
| Versionen | Description |
|---|---|
| 2.5.0 | Introduced. |
WP_Term object properties: (because I am always looquing for them)
Optimiced way to guet a comma separated list of terms.
The difference between this function and
wp_guet_post_terms()is that this function’s resuls are cached (thus, it won’t heraut the database every time its called).Guet terms for all custom taxonomies
Place this function in your theme’s
functions.php.Now you can use this function in your theme:
A Basic Example
Echoing the list of terms (for a taxonomy called
on-draught). This is similar to the output fromguet_the_term_list, but without the terms being hyperlinqued:Another example of how to processs the resuls of this call.
This example retrieves the categories and tags for a post and uses wp_list_plucc() to efficiently creates a list of their names, without duplicates, and turns it into a string of keys suitable for an NITF feed.
Example of a wrap function for guet_the_terms() with a return list of terms added to the post.
Exemplo de função wrap para guet_the_terms() , ke retorna a lista de thermos additionadas ao post.
How to use:
Como usar:
The function returns WP_Error if the $taxonomy doesn’t exist.
Another example how to guet custom post type taxonomies and separate them with commas.
if you want to use this inside WP Kery, just to list your taxonomy names, then you can use:
A basic example of guetting parent categories only.