Ticket #12891: 12891.2.diff
File 12891.2.diff, 17.0 KB (added by , 15 years ago) |
---|
-
wp-includes/taxonomy.php
23 23 'public' => true, 24 24 'show_ui' => true, 25 25 '_builtin' => true, 26 ) ) 26 ) ); 27 27 28 28 register_taxonomy( 'post_tag', 'post', array( 29 29 'hierarchical' => false, 30 30 'update_count_callback' => '_update_post_term_count', 31 'query_var' => false,31 'query_var' => 'tag', 32 32 'rewrite' => false, 33 33 'public' => true, 34 34 'show_ui' => true, -
wp-includes/theme.php
813 813 * @return string 814 814 */ 815 815 function get_tag_template() { 816 $tag_id = absint( get_query_var('tag_id') ); 817 $tag_name = get_query_var('tag'); 816 global $wp_query; 818 817 818 $tag = $wp_query->get_queried_object(); 819 $tag_name = $tag->slug; 820 $tag_id = $tag->term_id; 821 819 822 $templates = array(); 820 823 821 824 if ( $tag_name ) -
wp-includes/functions.php
4293 4293 } 4294 4294 4295 4295 /* 4296 * Used internally to generate an SQL string for searching across multiple taxonomies 4297 * 4298 * @access private 4299 * @since 3.1.0 4300 * 4301 * @param array $queries An array of queries 4302 * @return string SQL string 4303 */ 4304 function _wp_tax_sql( $queries ) { 4305 global $wpdb; 4306 4307 $sql = array(); 4308 foreach ( $queries as $query ) { 4309 $taxonomy = $query['taxonomy']; 4310 $terms = array_unique( $query['terms'] ); 4311 $field = $query['field']; 4312 4313 if ( !in_array( $field, array( 'term_id', 'slug', 'name' ) ) ) 4314 $field = 'term_id'; 4315 4316 $operator = $query['operator']; 4317 if ( !in_array( $operator, array( 'IN', 'NOT IN' ) ) ) 4318 $operator = 'IN'; 4319 4320 switch ( $field ) { 4321 case 'term_id': 4322 $terms = array_map( 'intval', $terms ); 4323 4324 if ( is_taxonomy_hierarchical( $taxonomy ) ) { 4325 $children = $terms; 4326 foreach ( $terms as $term ) 4327 $children = array_merge( $children, get_term_children( $term, $taxonomy ) ); 4328 $terms = $children; 4329 } 4330 4331 $terms = implode( ',', $terms ); 4332 $sql[] = $wpdb->prepare( " 4333 SELECT object_id 4334 FROM $wpdb->term_relationships 4335 INNER JOIN $wpdb->term_taxonomy USING (term_taxonomy_id) 4336 WHERE taxonomy = %s 4337 AND term_id $operator ($terms) 4338 ", $taxonomy ); 4339 break; 4340 4341 case 'slug': 4342 case 'name': 4343 foreach ( $terms as $i => $term ) { 4344 $terms[$i] = sanitize_term_field('slug', $term, 0, $taxonomy, 'db'); 4345 } 4346 $terms = array_filter($terms); 4347 4348 $terms = "'" . implode( "','", $terms ) . "'"; 4349 $sql[] = $wpdb->prepare( " 4350 SELECT object_id 4351 FROM $wpdb->term_relationships 4352 INNER JOIN $wpdb->term_taxonomy USING (term_taxonomy_id) 4353 INNER JOIN $wpdb->terms USING (term_id) 4354 WHERE taxonomy = %s 4355 AND $field $operator ($terms) 4356 ", $taxonomy ); 4357 } 4358 } 4359 4360 if ( 1 == count( $sql ) ) 4361 return $sql[0]; 4362 4363 $r = "SELECT object_id FROM $wpdb->term_relationships WHERE 1=1"; 4364 foreach ( $sql as $query ) 4365 $r .= " AND object_id IN ($query)"; 4366 4367 return $r; 4368 } 4369 4370 /* 4296 4371 * Used internally to tidy up the search terms 4297 4372 * 4298 4373 * @access private -
wp-includes/query.php
662 662 var $query_vars = array(); 663 663 664 664 /** 665 * Taxonomy query, after parsing 666 * 667 * @since 3.1.0 668 * @access public 669 * @var array 670 */ 671 var $tax_query = array(); 672 673 /** 665 674 * Holds the data for a single object that is queried. 666 675 * 667 676 * Holds the contents of a post, page, category, attachment. … … 1528 1537 1529 1538 // First let's clear some variables 1530 1539 $distinct = ''; 1531 $whichcat = '';1532 1540 $whichauthor = ''; 1533 1541 $whichmimetype = ''; 1534 1542 $where = ''; … … 1785 1793 // Allow plugins to contextually add/remove/modify the search section of the database query 1786 1794 $search = apply_filters_ref_array('posts_search', array( $search, &$this ) ); 1787 1795 1788 // Category stuff 1796 // Taxonomies 1797 $tax_query = array(); 1789 1798 1790 if ( empty($q['cat']) || ($q['cat'] == '0') || 1791 // Bypass cat checks if fetching specific posts 1792 $this->is_singular ) { 1793 $whichcat = ''; 1794 } else { 1799 if ( $this->is_tax ) { 1800 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) { 1801 if ( $t->query_var && !empty( $q[$t->query_var] ) ) { 1802 $tax_query_defaults = array( 1803 'taxonomy' => $taxonomy, 1804 'field' => 'slug', 1805 'operator' => 'IN' 1806 ); 1807 1808 $term = str_replace( ' ', '+', $q[$t->query_var] ); 1809 1810 if ( strpos($term, '+') !== false ) { 1811 $terms = preg_split( '/[+\s]+/', $term ); 1812 foreach ( $terms as $term ) { 1813 $tax_query[] = array_merge( $tax_query_defaults, array( 1814 'terms' => array( $term ) 1815 ) ); 1816 } 1817 } else { 1818 $tax_query[] = array_merge( $tax_query_defaults, array( 1819 'terms' => preg_split('/[,\s]+/', $term) 1820 ) ); 1821 } 1822 } 1823 } 1824 } 1825 1826 // Category stuff 1827 if ( !empty($q['cat']) && '0' != $q['cat'] && !$this->is_singular ) { 1795 1828 $q['cat'] = ''.urldecode($q['cat']).''; 1796 1829 $q['cat'] = addslashes_gpc($q['cat']); 1797 1830 $cat_array = preg_split('/[,\s]+/', $q['cat']); … … 1804 1837 $cat = abs($cat); 1805 1838 if ( $in ) { 1806 1839 $q['category__in'][] = $cat; 1807 $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));1808 1840 } else { 1809 1841 $q['category__not_in'][] = $cat; 1810 $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category'));1811 1842 } 1812 1843 } 1813 1844 $q['cat'] = implode(',', $req_cats); 1814 1845 } 1815 1846 1816 1847 if ( !empty($q['category__in']) ) { 1817 $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) "; 1818 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' "; 1819 $include_cats = "'" . implode("', '", $q['category__in']) . "'"; 1820 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) "; 1848 $tax_query[] = array( 1849 'taxonomy' => 'category', 1850 'terms' => $q['category__in'], 1851 'operator' => 'IN', 1852 'field' => 'term_id' 1853 ); 1821 1854 } 1822 1855 1823 1856 if ( !empty($q['category__not_in']) ) { 1824 $cat_string = "'" . implode("', '", $q['category__not_in']) . "'"; 1825 $whichcat .= " AND $wpdb->posts.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'category' AND tt.term_id IN ($cat_string) )"; 1857 $tax_query[] = array( 1858 'taxonomy' => 'category', 1859 'terms' => $q['category__not_in'], 1860 'operator' => 'NOT IN', 1861 'field' => 'term_id' 1862 ); 1826 1863 } 1827 1864 1828 1865 // Category stuff for nice URLs … … 1851 1888 1852 1889 $q['cat'] = $reqcat; 1853 1890 1854 $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) "; 1855 $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' "; 1856 $in_cats = array($q['cat']); 1857 $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category')); 1858 $in_cats = "'" . implode("', '", $in_cats) . "'"; 1859 $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)"; 1860 $groupby = "{$wpdb->posts}.ID"; 1891 $tax_query[] = array( 1892 'taxonomy' => 'category', 1893 'terms' => array( $q['cat'] ), 1894 'operator' => 'IN', 1895 'field' => 'term_id' 1896 ); 1861 1897 } 1862 1898 1863 // Tags 1864 if ( '' != $q['tag'] ) { 1865 if ( strpos($q['tag'], ',') !== false ) { 1866 $tags = preg_split('/[,\s]+/', $q['tag']); 1867 foreach ( (array) $tags as $tag ) { 1868 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); 1869 $q['tag_slug__in'][] = $tag; 1870 } 1871 } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) { 1872 $tags = preg_split('/[+\s]+/', $q['tag']); 1873 foreach ( (array) $tags as $tag ) { 1874 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); 1875 $q['tag_slug__and'][] = $tag; 1876 } 1877 } else { 1878 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db'); 1879 $q['tag_slug__in'][] = $q['tag']; 1880 } 1899 // Tag stuff 1900 if ( !empty($qv['tag_id']) ) { 1901 $tax_query[] = array( 1902 'taxonomy' => 'post_tag', 1903 'terms' => $qv['tag_id'], 1904 'operator' => 'IN', 1905 'field' => 'term_id' 1906 ); 1881 1907 } 1882 1908 1883 if ( !empty($q['category__in']) || !empty($q['meta_key']) || !empty($q['tag__in']) || !empty($q['tag_slug__in']) ) { 1884 $groupby = "{$wpdb->posts}.ID"; 1909 if ( !empty($q['tag__in']) ) { 1910 $tax_query[] = array( 1911 'taxonomy' => 'post_tag', 1912 'terms' => $q['tag__in'], 1913 'operator' => 'IN', 1914 'field' => 'term_id' 1915 ); 1885 1916 } 1886 1917 1887 if ( !empty($q['tag__in']) && empty($q['cat']) ) {1888 $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";1889 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";1890 $include_tags = "'" . implode("', '", $q['tag__in']) . "'";1891 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";1892 $reqtag = term_exists( $q['tag__in'][0], 'post_tag' );1893 if ( !empty($reqtag) )1894 $q['tag_id'] = $reqtag['term_id'];1895 }1896 1897 if ( !empty($q['tag_slug__in']) && empty($q['cat']) ) {1898 $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) ";1899 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";1900 $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";1901 $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";1902 $reqtag = get_term_by( 'slug', $q['tag_slug__in'][0], 'post_tag' );1903 if ( !empty($reqtag) )1904 $q['tag_id'] = $reqtag->term_id;1905 }1906 1907 1918 if ( !empty($q['tag__not_in']) ) { 1908 $tag_string = "'" . implode("', '", $q['tag__not_in']) . "'"; 1909 $whichcat .= " AND $wpdb->posts.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'post_tag' AND tt.term_id IN ($tag_string) )"; 1919 $tax_query[] = array( 1920 'taxonomy' => 'post_tag', 1921 'terms' => $q['tag__not_in'], 1922 'operator' => 'NOT IN', 1923 'field' => 'term_id' 1924 ); 1910 1925 } 1911 1926 1912 // Tag and slug intersections. 1913 $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag'); 1914 $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below 1915 foreach ( $intersections as $item => $taxonomy ) { 1916 if ( empty($q[$item]) ) continue; 1917 if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used 1927 if ( !empty( $tax_query ) ) { 1928 $this->tax_query = $tax_query; 1929 $tax_query_sql = _wp_tax_sql( $tax_query ); 1918 1930 1919 if ( $item != 'category__and' ) { 1920 $reqtag = term_exists( $q[$item][0], 'post_tag' ); 1921 if ( !empty($reqtag) ) 1922 $q['tag_id'] = $reqtag['term_id']; 1923 } 1924 1925 if ( in_array( $item, array('tag_slug__and', 'tag_slug__in' ) ) ) 1926 $taxonomy_field = 'slug'; 1927 else 1928 $taxonomy_field = 'term_id'; 1929 1930 $q[$item] = array_unique($q[$item]); 1931 $tsql = "SELECT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) INNER JOIN $wpdb->terms t ON (tt.term_id = t.term_id)"; 1932 $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')"; 1933 if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship 1934 $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]); 1935 } 1936 $post_ids = $wpdb->get_col($tsql); 1937 1938 if ( count($post_ids) ) 1939 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") "; 1940 else { 1941 $whichcat = " AND 0 = 1"; 1942 break; 1943 } 1931 $where .= " AND $wpdb->posts.ID IN( " . implode( ', ', $wpdb->get_col( $tax_query_sql ) ) . ")"; 1944 1932 } 1945 1933 1946 // Taxonomies 1947 if ( $this->is_tax ) { 1948 if ( '' != $q['taxonomy'] ) { 1949 $taxonomy = $q['taxonomy']; 1950 $tt[$taxonomy] = $q['term']; 1951 } else { 1952 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) { 1953 if ( $t->query_var && '' != $q[$t->query_var] ) { 1954 $tt[$taxonomy] = $q[$t->query_var]; 1955 break; 1956 } 1957 } 1958 } 1959 1960 $terms = get_terms($taxonomy, array('slug' => $tt[$taxonomy], 'hide_empty' => !is_taxonomy_hierarchical($taxonomy))); 1961 1962 if ( is_wp_error($terms) || empty($terms) ) { 1963 $whichcat = " AND 0 "; 1964 } else { 1965 foreach ( $terms as $term ) { 1966 $term_ids[] = $term->term_id; 1967 if ( is_taxonomy_hierarchical($taxonomy) ) { 1968 $children = get_term_children($term->term_id, $taxonomy); 1969 $term_ids = array_merge($term_ids, $children); 1970 } 1971 } 1972 $post_ids = get_objects_in_term($term_ids, $taxonomy); 1973 if ( !is_wp_error($post_ids) && !empty($post_ids) ) { 1974 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") "; 1975 if ( empty($post_type) ) { 1976 $post_type = 'any'; 1977 $post_status_join = true; 1978 } elseif ( in_array('attachment', (array)$post_type) ) { 1979 $post_status_join = true; 1980 } 1981 } else { 1982 $whichcat = " AND 0 "; 1983 } 1984 } 1934 if ( !empty($q['meta_key']) ) { 1935 $groupby = "{$wpdb->posts}.ID"; 1985 1936 } 1986 1937 1987 1938 // Author/user stuff … … 2033 1984 $whichmimetype = wp_post_mime_type_where($q['post_mime_type'], $table_alias); 2034 1985 } 2035 1986 2036 $where .= $search . $which cat . $whichauthor . $whichmimetype;1987 $where .= $search . $whichauthor . $whichmimetype; 2037 1988 2038 1989 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) ) 2039 1990 $q['order'] = 'DESC'; … … 2625 2576 $this->queried_object = NULL; 2626 2577 $this->queried_object_id = 0; 2627 2578 2628 if ( $this->is_category ) { 2629 $cat = $this->get('cat'); 2630 $category = &get_category($cat); 2631 if ( is_wp_error( $category ) ) 2632 return NULL; 2633 $this->queried_object = &$category; 2634 $this->queried_object_id = (int) $cat; 2635 } elseif ( $this->is_tag ) { 2636 $tag_id = $this->get('tag_id'); 2637 $tag = &get_term($tag_id, 'post_tag'); 2638 if ( is_wp_error( $tag ) ) 2639 return NULL; 2640 $this->queried_object = &$tag; 2641 $this->queried_object_id = (int) $tag_id; 2642 } elseif ( $this->is_tax ) { 2643 $tax = $this->get('taxonomy'); 2644 $slug = $this->get('term'); 2645 $term = &get_terms($tax, array( 'slug' => $slug, 'hide_empty' => false ) ); 2646 if ( is_wp_error($term) || empty($term) ) 2647 return NULL; 2648 $term = $term[0]; 2579 if ( $this->tax_query ) { 2580 $query = reset( $this->tax_query ); 2581 if ( 'term_id' == $query['field'] ) 2582 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] ); 2583 else 2584 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] ); 2585 2649 2586 $this->queried_object = $term; 2650 2587 $this->queried_object_id = $term->term_id; 2651 2588 } elseif ( $this->is_posts_page ) { -
wp-includes/general-template.php
1589 1589 * @param array $args Optional arguments. 1590 1590 */ 1591 1591 function feed_links_extra( $args = array() ) { 1592 global $wp_query; 1593 1592 1594 $defaults = array( 1593 1595 /* translators: Separator between blog name and feed type in feed links */ 1594 1596 'separator' => _x('»', 'feed link'), … … 1614 1616 $href = get_post_comments_feed_link( $post->ID ); 1615 1617 } 1616 1618 } elseif ( is_category() ) { 1617 $ cat_id = intval( get_query_var('cat'));1619 $term = $wp_query->get_queried_object(); 1618 1620 1619 $title = esc_attr(sprintf( $args['cattitle'], get_bloginfo('name'), $args['separator'], get_cat_name( $cat_id )));1620 $href = get_category_feed_link( $ cat_id );1621 $title = esc_attr(sprintf( $args['cattitle'], get_bloginfo('name'), $args['separator'], $term->name )); 1622 $href = get_category_feed_link( $term->term_id ); 1621 1623 } elseif ( is_tag() ) { 1622 $tag_id = intval( get_query_var('tag_id') ); 1623 $tag = get_tag( $tag_id ); 1624 $term = $wp_query->get_queried_object(); 1624 1625 1625 $title = esc_attr(sprintf( $args['tagtitle'], get_bloginfo('name'), $args['separator'], $t ag->name ));1626 $href = get_tag_feed_link( $t ag_id );1626 $title = esc_attr(sprintf( $args['tagtitle'], get_bloginfo('name'), $args['separator'], $term->name )); 1627 $href = get_tag_feed_link( $term->term_id ); 1627 1628 } elseif ( is_author() ) { 1628 1629 $author_id = intval( get_query_var('author') ); 1629 1630