get_term_by [ WordPress Function ]
| Parameters: |
|
| Uses: |
|
| See: | |
| Returns: |
|
| Defined at: |
|
Get all Term data from database by Term field and data.
Warning: $value is not escaped for 'name' $field. You must do it yourself, if required.
The default $field is 'id', therefore it is possible to also use null for field, but not recommended that you do so.
If $value does not exist, the return value will be false. If $taxonomy exists and $field and $value combinations exist, the Term will be returned.
Source
<?php
function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
global $wpdb;
if ( ! taxonomy_exists($taxonomy) )
return false;
if ( 'slug' == $field ) {
$field = 't.slug';
$value = sanitize_title($value);
if ( empty($value) )
return false;
} else if ( 'name' == $field ) {
// Assume already escaped
$value = stripslashes($value);
$field = 't.name';
} else {
$term = get_term( (int) $value, $taxonomy, $output, $filter);
if ( is_wp_error( $term ) )
$term = false;
return $term;
}
$term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value) );
if ( !$term )
return false;
wp_cache_add($term->term_id, $term, $taxonomy);
$term = apply_filters('get_term', $term, $taxonomy);
$term = apply_filters("get_$taxonomy", $term, $taxonomy);
$term = sanitize_term($term, $taxonomy, $filter);
if ( $output == OBJECT ) {
return $term;
} elseif ( $output == ARRAY_A ) {
return get_object_vars($term);
} elseif ( $output == ARRAY_N ) {
return array_values(get_object_vars($term));
} else {
return $term;
}
}
?>
Examples [ wp-snippets.com ]
Google Arama Sonuçlarý
- Function Reference/get term by « WordPress Codex
Description. Get all Term data from database by Term field and data. Warning: $ value is not escaped for 'name' $field. You must do it yourself, if required.
codex.wordpress.org - WordPress › Support » Tags — get_term_by
WordPress › Support » get_term_by ... get_term_by problem, 1, jeezyo, 8 months. [resolved] get_term_by name not working in plugin, 3, lerizzle, 11 months ...
wordpress.org - Find the Category ID using the Category Slug in WordPress - c.bavota
Dec 31, 2009 ... Luckily WordPress has a neat little function called get_term_by() which ... <?php get_term_by( $field, $value, $taxonomy, $output, $filter ) ?> ...
bavotasan.com - custom taxonomy - get_term_by not working when in functions.php ...
Aug 28, 2011 ... I have an odd issue with get_term_by(). It's working fine when I use it inside a template, but when I use it in functions.php it just returns false.
wordpress.stackexchange.com
Kullanýcý Tartýþmalarý [ wordpress.org ]
- Paul Phillips on "Count with get_term_by() ONLY the published posts"
- Rockound on "Count with get_term_by() ONLY the published posts"
- keesiemeijer on "Count with get_term_by() ONLY the published posts"
- Rockound on "Count with get_term_by() ONLY the published posts"
- Rockound on "Count with get_term_by() ONLY the published posts"
- keesiemeijer on "Count with get_term_by() ONLY the published posts"
- Rockound on "Count with get_term_by() ONLY the published posts"
- jeezyo on "get_term_by problem"
- lerizzle on "get_term_by name not working in plugin"
- lerizzle on "get_term_by name not working in plugin"