Add get_terms() and get_term(). Move more of tagging to taxonomy. see #4189
git-svn-id: http://svn.automattic.com/wordpress/trunk@5521 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
e623c78708
commit
0b795ff92b
|
@ -311,8 +311,8 @@ function wp_generate_tag_cloud( $tags, $args = '' ) {
|
|||
return;
|
||||
$counts = $tag_links = array();
|
||||
foreach ( (array) $tags as $tag ) {
|
||||
$counts[$tag->cat_name] = $tag->tag_count;
|
||||
$tag_links[$tag->cat_name] = get_tag_link( $tag->cat_ID );
|
||||
$counts[$tag->name] = $tag->count;
|
||||
$tag_links[$tag->name] = get_tag_link( $tag->term_id );
|
||||
}
|
||||
|
||||
$min_count = min($counts);
|
||||
|
@ -384,20 +384,19 @@ function walk_category_dropdown_tree() {
|
|||
|
||||
function get_tag_link( $tag_id ) {
|
||||
global $wp_rewrite;
|
||||
$catlink = $wp_rewrite->get_tag_permastruct();
|
||||
$taglink = $wp_rewrite->get_tag_permastruct();
|
||||
|
||||
$category = &get_category($tag_id);
|
||||
$category_nicename = $category->category_nicename;
|
||||
$tag = &get_term($tag_id, 'post_tag');
|
||||
$slug = $tag->slug;
|
||||
|
||||
if ( empty($catlink) ) {
|
||||
$file = get_option('home') . '/';
|
||||
$catlink = $file . '?tag=' . $category_nicename;
|
||||
$taglink = $file . '?tag=' . $slug;
|
||||
} else {
|
||||
|
||||
$catlink = str_replace('%tag%', $category_nicename, $catlink);
|
||||
$catlink = get_option('home') . user_trailingslashit($catlink, 'category');
|
||||
$taglink = str_replace('%tag%', $slug, $taglink);
|
||||
$taglink = get_option('home') . user_trailingslashit($taglink, 'category');
|
||||
}
|
||||
return apply_filters('tag_link', $catlink, $tag_id);
|
||||
return apply_filters('tag_link', $taglink, $tag_id);
|
||||
}
|
||||
|
||||
function get_the_tags( $id = 0 ) {
|
||||
|
@ -426,7 +425,7 @@ function the_tags( $before = 'Tags: ', $sep = ', ', $after = '' ) {
|
|||
|
||||
$tag_list = $before;
|
||||
foreach ( $tags as $tag )
|
||||
$tag_links[] = '<a href="' . get_tag_link($tag->cat_ID) . '">' . $tag->cat_name . '</a>';
|
||||
$tag_links[] = '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->slug . '</a>';
|
||||
|
||||
$tag_links = join( $sep, $tag_links );
|
||||
$tag_links = apply_filters( 'the_tags', $tag_links );
|
||||
|
|
|
@ -353,71 +353,13 @@ function _get_category_hierarchy() {
|
|||
function &get_tags($args = '') {
|
||||
global $wpdb, $category_links;
|
||||
|
||||
$defaults = array('orderby' => 'name', 'order' => 'ASC',
|
||||
'hide_empty' => true, 'exclude' => '', 'include' => '',
|
||||
'number' => '');
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
if ( 'count' == $args['orderby'] )
|
||||
$args['orderby'] = 'tag_count';
|
||||
else
|
||||
$args['orderby'] = "cat_" . $args['orderby']; // restricts order by to cat_ID and cat_name fields
|
||||
$args['number'] = (int) $args['number'];
|
||||
extract($args);
|
||||
|
||||
$key = md5( serialize( $args ) );
|
||||
if ( $cache = wp_cache_get( 'get_tags', 'category' ) )
|
||||
if ( isset( $cache[ $key ] ) )
|
||||
return apply_filters('get_tags', $cache[$key], $args);
|
||||
|
||||
$where = 'cat_ID > 0';
|
||||
$inclusions = '';
|
||||
if ( !empty($include) ) {
|
||||
$child_of = 0; //ignore child_of and exclude params if using include
|
||||
$exclude = '';
|
||||
$incategories = preg_split('/[\s,]+/',$include);
|
||||
if ( count($incategories) ) {
|
||||
foreach ( $incategories as $incat ) {
|
||||
if (empty($inclusions))
|
||||
$inclusions = ' AND ( cat_ID = ' . intval($incat) . ' ';
|
||||
else
|
||||
$inclusions .= ' OR cat_ID = ' . intval($incat) . ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($inclusions))
|
||||
$inclusions .= ')';
|
||||
$where .= $inclusions;
|
||||
|
||||
$exclusions = '';
|
||||
if ( !empty($exclude) ) {
|
||||
$excategories = preg_split('/[\s,]+/',$exclude);
|
||||
if ( count($excategories) ) {
|
||||
foreach ( $excategories as $excat ) {
|
||||
if (empty($exclusions))
|
||||
$exclusions = ' AND ( cat_ID <> ' . intval($excat) . ' ';
|
||||
else
|
||||
$exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($exclusions))
|
||||
$exclusions .= ')';
|
||||
$exclusions = apply_filters('list_tags_exclusions', $exclusions, $args );
|
||||
$where .= $exclusions;
|
||||
|
||||
if ( $hide_empty )
|
||||
$where .= ' AND tag_count > 0';
|
||||
|
||||
$where .= ' AND ( type & ' . TAXONOMY_TAG . ' != 0 ) ';
|
||||
|
||||
if ( !empty($number) )
|
||||
$number = 'LIMIT ' . $number;
|
||||
else
|
||||
$number = '';
|
||||
|
||||
$tags = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE $where ORDER BY $orderby $order $number");
|
||||
$tags = get_terms('post_tag');
|
||||
|
||||
if ( empty($tags) )
|
||||
return array();
|
||||
|
|
|
@ -160,6 +160,7 @@ function wp_title($sep = '»', $display = true) {
|
|||
global $wpdb, $wp_locale, $wp_query;
|
||||
|
||||
$cat = get_query_var('cat');
|
||||
$tag = get_query_var('tag_id');
|
||||
$p = get_query_var('p');
|
||||
$name = get_query_var('name');
|
||||
$category_name = get_query_var('category_name');
|
||||
|
@ -188,6 +189,12 @@ function wp_title($sep = '»', $display = true) {
|
|||
$title = apply_filters('single_cat_title', $title);
|
||||
}
|
||||
|
||||
if ( !empty($tag) ) {
|
||||
$tag = get_term($tag, 'post_tag');
|
||||
if ( ! empty($tag->name) )
|
||||
$title = apply_filters('single_tag_title', $tag->slug);
|
||||
}
|
||||
|
||||
// If there's an author
|
||||
if ( !empty($author) ) {
|
||||
$title = get_userdata($author);
|
||||
|
|
|
@ -94,8 +94,8 @@ function is_tag( $slug = '' ) {
|
|||
if ( empty( $slug ) )
|
||||
return true;
|
||||
|
||||
$cat_obj = $wp_query->get_queried_object();
|
||||
if ( $category == $cat_obj->category_nicename )
|
||||
$tag_obj = $wp_query->get_queried_object();
|
||||
if ( $slug == $tag_obj->slug )
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
@ -875,17 +875,17 @@ class WP_Query {
|
|||
}
|
||||
|
||||
if ( '' != $q['tag'] ) {
|
||||
$reqcat= get_category_by_slug( $q['tag'] );
|
||||
if ( !empty($reqcat) )
|
||||
$reqcat = $reqcat->cat_ID;
|
||||
$reqtag = is_term( $q['tag'], 'post_tag' );
|
||||
if ( !empty($reqtag) )
|
||||
$reqtag = $reqtag['term_id'];
|
||||
else
|
||||
$reqcat = 0;
|
||||
|
||||
$q['cat'] = $reqcat;
|
||||
$reqtag = 0;
|
||||
|
||||
$q['tag_id'] = $reqtag;
|
||||
// TODO: use term taxonomy
|
||||
$tables = ", $wpdb->post2cat, $wpdb->categories";
|
||||
$join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) ";
|
||||
$whichcat = " AND category_id IN ({$q['cat']}) AND rel_type = 'tag' ";
|
||||
$join = " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
|
||||
$whichcat = " AND $wpdb->term_taxonomy.term_id IN ({$q['tag_id']}) AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
|
||||
$groupby = "{$wpdb->posts}.ID";
|
||||
}
|
||||
|
||||
|
|
|
@ -125,6 +125,7 @@ function wp_set_object_terms($object_id, $terms, $taxonomies, $append = false) {
|
|||
if ( $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id = '$id'") )
|
||||
continue;
|
||||
$wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$object_id', '$id')");
|
||||
$wpdb->query("UPDATE $wpdb->term_taxonomy SET count = count + 1 WHERE term_taxonomy_id = $id");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -133,6 +134,7 @@ function wp_set_object_terms($object_id, $terms, $taxonomies, $append = false) {
|
|||
if ( $delete_terms ) {
|
||||
$delete_terms = "'" . implode("', '", $delete_terms) . "'";
|
||||
$wpdb->query("DELETE FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ($delete_terms)");
|
||||
$wpdb->query("UPDATE $wpdb->term_taxonomy SET count = count - 1 WHERE term_taxonomy_id IN ($delete_terms)");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,7 +155,7 @@ function get_object_terms($object_id, $taxonomy) {
|
|||
$taxonomies = "'" . implode("', '", $taxonomies) . "'";
|
||||
$object_ids = implode(', ', $object_ids);
|
||||
|
||||
if ( $taxonomy_data = $wpdb->get_results("SELECT t.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids)") ) {
|
||||
if ( $taxonomy_data = $wpdb->get_results("SELECT t.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) ORDER BY t.name") ) {
|
||||
if ($single_taxonomy && $single_object) {
|
||||
// Just one kind of taxonomy for one object.
|
||||
return $taxonomy_data;
|
||||
|
@ -177,4 +179,107 @@ function get_object_terms($object_id, $taxonomy) {
|
|||
}
|
||||
}
|
||||
|
||||
function &get_terms($taxonomies, $args = '') {
|
||||
global $wpdb;
|
||||
|
||||
if ( !is_array($taxonomies) )
|
||||
$taxonomies = array($taxonomies);
|
||||
$in_taxonomies = "'" . implode("', '", $taxonomies) . "'";
|
||||
|
||||
$defaults = array('orderby' => 'name', 'order' => 'ASC',
|
||||
'hide_empty' => true, 'exclude' => '', 'include' => '',
|
||||
'number' => '');
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
$args['number'] = (int) $args['number'];
|
||||
extract($args);
|
||||
|
||||
if ( 'count' == $orderby )
|
||||
$orderby = 'tt.count';
|
||||
else if ( 'name' == $orderby )
|
||||
$orderby = 't.name';
|
||||
|
||||
$where = '';
|
||||
$inclusions = '';
|
||||
if ( !empty($include) ) {
|
||||
$exclude = '';
|
||||
$interms = preg_split('/[\s,]+/',$include);
|
||||
if ( count($interms) ) {
|
||||
foreach ( $interms as $interm ) {
|
||||
if (empty($inclusions))
|
||||
$inclusions = ' AND ( t.term_id = ' . intval($interm) . ' ';
|
||||
else
|
||||
$inclusions .= ' OR t.term_id = ' . intval($interm) . ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( !empty($inclusions) )
|
||||
$inclusions .= ')';
|
||||
$where .= $inclusions;
|
||||
|
||||
$exclusions = '';
|
||||
if ( !empty($exclude) ) {
|
||||
$exterms = preg_split('/[\s,]+/',$exclude);
|
||||
if ( count($exterms) ) {
|
||||
foreach ( $exterms as $exterm ) {
|
||||
if (empty($exclusions))
|
||||
$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
|
||||
else
|
||||
$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( !empty($exclusions) )
|
||||
$exclusions .= ')';
|
||||
$exclusions = apply_filters('list_terms_exclusions', $exclusions, $args );
|
||||
$where .= $exclusions;
|
||||
|
||||
if ( $hide_empty )
|
||||
$where .= ' AND tt.count > 0';
|
||||
|
||||
if ( !empty($number) )
|
||||
$number = 'LIMIT ' . $number;
|
||||
else
|
||||
$number = '';
|
||||
|
||||
$terms = $wpdb->get_results("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 IN ($in_taxonomies) $where ORDER BY $orderby $order $number");
|
||||
|
||||
if ( empty($terms) )
|
||||
return array();
|
||||
|
||||
$terms = apply_filters('get_terms', $terms, $taxonomies, $args);
|
||||
return $terms;
|
||||
}
|
||||
|
||||
function &get_term(&$term, $taxonomy, $output = OBJECT) {
|
||||
global $wpdb;
|
||||
|
||||
if ( empty($term) )
|
||||
return null;
|
||||
|
||||
if ( is_object($term) ) {
|
||||
wp_cache_add($term->term_id, $term, "term:$taxonomy");
|
||||
$_term = $term;
|
||||
} else {
|
||||
$term = (int) $term;
|
||||
if ( ! $_term = wp_cache_get($term, "term:$taxonomy") ) {
|
||||
$_term = $wpdb->get_row("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 = '$taxonomy' AND t.term_id = '$term' LIMIT 1");
|
||||
wp_cache_add($term, $_term, "term:$taxonomy");
|
||||
}
|
||||
}
|
||||
|
||||
$_term = apply_filters('get_term', $_term, $taxonomy);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue