Ticket #12891: 12891.diff
File 12891.diff, 7.1 KB (added by , 15 years ago) |
---|
-
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) $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 = implode( ',', array_map( 'intval', $terms ) ); 4323 $terms = $wpdb->prepare( " 4324 SELECT term_taxonomy_id 4325 FROM $wpdb->term_taxonomy 4326 WHERE taxonomy = %s 4327 AND term_id IN ( $terms ) 4328 ", $taxonomy ); 4329 break; 4330 4331 case 'slug': 4332 case 'name': 4333 $terms = "'" . implode( "','", esc_sql( $terms ) ) . "'"; 4334 $terms = $wpdb->prepare( " 4335 SELECT term_taxonomy_id 4336 FROM $wpdb->term_taxonomy 4337 INNER JOIN $wpdb->terms USING ( term_id ) 4338 WHERE taxonomy = %s 4339 AND $field IN ( $terms ) 4340 ", $taxonomy ); 4341 } 4342 4343 $sql[] = " AND term_taxonomy_id $operator ( $terms )"; 4344 } 4345 4346 return "SELECT object_id FROM $wpdb->term_relationships WHERE 1=1" . implode( '', $sql ); 4347 } 4348 4349 /* 4296 4350 * Used internally to tidy up the search terms 4297 4351 * 4298 4352 * @access private -
wp-includes/query.php
1780 1780 $search = apply_filters_ref_array('posts_search', array( $search, &$this ) ); 1781 1781 1782 1782 // Category stuff 1783 1784 1783 if ( empty($q['cat']) || ($q['cat'] == '0') || 1785 1784 // Bypass cat checks if fetching specific posts 1786 1785 $this->is_singular ) { … … 1808 1807 } 1809 1808 1810 1809 if ( !empty($q['category__in']) ) { 1811 $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) "; 1812 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' "; 1813 $include_cats = "'" . implode("', '", $q['category__in']) . "'"; 1814 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) "; 1810 $tax_query[] = array( 1811 'taxonomy' => 'category', 1812 'terms' => $q['category__in'], 1813 'operator' => 'IN', 1814 'field' => 'term_id' 1815 ); 1815 1816 } 1816 1817 1817 1818 if ( !empty($q['category__not_in']) ) { 1818 $cat_string = "'" . implode("', '", $q['category__not_in']) . "'"; 1819 $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) )"; 1819 $tax_query[] = array( 1820 'taxonomy' => 'category', 1821 'terms' => $q['category__not_in'], 1822 'operator' => 'NOT IN', 1823 'field' => 'term_id' 1824 ); 1820 1825 } 1821 1826 1822 1827 // Category stuff for nice URLs … … 1845 1850 1846 1851 $q['cat'] = $reqcat; 1847 1852 1848 $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) ";1849 $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";1850 1853 $in_cats = array($q['cat']); 1851 1854 $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category')); 1852 $in_cats = "'" . implode("', '", $in_cats) . "'"; 1853 $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)"; 1854 $groupby = "{$wpdb->posts}.ID"; 1855 1856 $tax_query[] = array( 1857 'taxonomy' => 'category', 1858 'terms' => $in_cats, 1859 'operator' => 'IN', 1860 'field' => 'term_id' 1861 ); 1855 1862 } 1856 1863 1857 1864 // Tags … … 1874 1881 } 1875 1882 } 1876 1883 1877 if ( !empty($q['category__in']) || !empty($q['meta_key']) || !empty($q['tag__in']) || !empty($q['tag_slug__in']) ) { 1878 $groupby = "{$wpdb->posts}.ID"; 1879 } 1884 if ( !empty($q['tag__in']) && empty($q['cat']) ) { 1885 $tax_query[] = array( 1886 'taxonomy' => 'post_tag', 1887 'terms' => $q['tag__in'], 1888 'operator' => 'IN', 1889 'field' => 'term_id' 1890 ); 1880 1891 1881 if ( !empty($q['tag__in']) && empty($q['cat']) ) {1882 $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) ";1883 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";1884 $include_tags = "'" . implode("', '", $q['tag__in']) . "'";1885 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";1886 1892 $reqtag = term_exists( $q['tag__in'][0], 'post_tag' ); 1887 1893 if ( !empty($reqtag) ) 1888 1894 $q['tag_id'] = $reqtag['term_id']; 1889 1895 } 1890 1896 1891 1897 if ( !empty($q['tag_slug__in']) && empty($q['cat']) ) { 1892 $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) "; 1893 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' "; 1894 $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'"; 1895 $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) "; 1898 $tax_query[] = array( 1899 'taxonomy' => 'post_tag', 1900 'terms' => $q['tag_slug__in'], 1901 'operator' => 'IN', 1902 'field' => 'slug' 1903 ); 1904 1896 1905 $reqtag = get_term_by( 'slug', $q['tag_slug__in'][0], 'post_tag' ); 1897 1906 if ( !empty($reqtag) ) 1898 1907 $q['tag_id'] = $reqtag->term_id; 1899 1908 } 1900 1909 1901 1910 if ( !empty($q['tag__not_in']) ) { 1902 $tag_string = "'" . implode("', '", $q['tag__not_in']) . "'"; 1903 $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) )"; 1911 $tax_query[] = array( 1912 'taxonomy' => 'post_tag', 1913 'terms' => $q['tag__not_in'], 1914 'operator' => 'NOT IN', 1915 'field' => 'term_id' 1916 ); 1904 1917 } 1905 1918 1919 if ( !empty( $tax_query ) ) { 1920 $tax_query_sql = _wp_tax_sql( $tax_query ); 1921 1922 $where .= " AND $wpdb->posts.ID IN( " . implode( ', ', $wpdb->get_col( $tax_query_sql ) ) . ")"; 1923 } 1924 1906 1925 // Tag and slug intersections. 1907 1926 $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag'); 1908 1927 $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below … … 1978 1997 } 1979 1998 } 1980 1999 2000 if ( !empty($q['meta_key']) ) { 2001 $groupby = "{$wpdb->posts}.ID"; 2002 } 2003 1981 2004 // Author/user stuff 1982 2005 1983 2006 if ( empty($q['author']) || ($q['author'] == '0') ) {