Ticket #9951: query.diff
File query.diff, 4.1 KB (added by , 16 years ago) |
---|
-
query.php
1395 1395 $qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']); 1396 1396 $this->is_tag = true; 1397 1397 } 1398 1399 //Custom Taxonomies 1400 $this->custom_taxonomies = array(); 1401 $this->custom_taxonomies__not_in = array(); 1402 1403 //loop through registered taxonomies to check for respective query vars 1404 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) { 1405 if( $taxonomy == 'category' || $taxonomy == 'post_tag' || $taxonomy == 'link_category' ) continue; 1406 if( array_key_exists( $taxonomy . '__in', $qv ) ) { 1407 $is = 'is_' . $taxonomy; 1408 $this->$is = true; 1409 $this->custom_taxonomies[$taxonomy . '__in'] = $taxonomy; 1410 } 1411 elseif( array_key_exists( $taxonomy . '__not_in', $qv ) ) { 1412 $is = 'is_' . $taxonomy; 1413 $this->$is = true; 1414 $this->custom_taxonomies[$taxonomy . '__not_in'] = $taxonomy; 1415 } 1416 } 1398 1417 1399 1418 if ( empty($qv['taxonomy']) || empty($qv['term']) ) { 1400 1419 $this->is_tax = false; … … 1901 1920 $whichcat .= " AND $wpdb->posts.ID NOT IN ('" . implode("', '", $ids) . "')"; 1902 1921 } 1903 1922 } 1923 1924 //Custom Taxonomies 1925 foreach( (array) $this->custom_taxonomies as $var => $custom_tax ) { 1926 $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) "; 1927 $reqtag = is_term( $q[$var][0], $custom_tax ); 1928 $distinct = 'DISTINCT'; 1929 } 1930 1931 //Custom Taxonomy Not In 1932 foreach( (array) $this->custom_taxonomies__not_in as $var => $custom_tax ) { 1933 if ( $wpdb->has_cap( 'subqueries' ) ) { 1934 $tax_string = "'" . implode("', '", $q[$var]) . "'"; 1935 $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 = '$custom_tax' AND tt.term_id IN ($tax_string) )"; 1936 } else { 1937 $ids = get_objects_in_term($q[$var], $custom_tax); 1938 if ( !is_wp_error($ids) && is_array($ids) && count($ids) > 0 ) 1939 $whichcat .= " AND $wpdb->posts.ID NOT IN ('" . implode("', '", $ids) . "')"; 1940 } 1941 } 1904 1942 1905 // Tag and slug intersections. 1943 // Tag and slug intersections. Also merge with custom taxonomies 1906 1944 $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag'); 1945 $intersections = array_merge( $intersections, (array) $this->custom_taxonomies ); 1907 1946 $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below 1908 1947 foreach ($intersections as $item => $taxonomy) { 1909 1948 if ( empty($q[$item]) ) continue; 1910 if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used 1911 1949 1912 1950 if ( $item != 'category__and' ) { 1913 1951 $reqtag = is_term( $q[$item][0], 'post_tag' ); 1914 1952 if ( !empty($reqtag) ) 1915 1953 $q['tag_id'] = $reqtag['term_id']; 1916 1954 } 1917 1955 1918 if ( in_array( $item, array('tag_slug__and', 'tag_slug__in' ) ) ) 1919 $taxonomy_field = 'slug'; 1920 else 1921 $taxonomy_field = 'term_id'; 1956 $taxonomy_field = $item != ('tag_slug__and' || 'tag_slug__in') ? 'slug' : 'term_id'; 1922 1957 1923 1958 $q[$item] = array_unique($q[$item]); 1924 1959 $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)"; 1925 $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')"; 1960 $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')"; 1926 1961 if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship 1927 1962 $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]); 1928 1963 } … … 2697 2732 return true; 2698 2733 } 2699 2734 2700 ?> 2735 ?> 2736 No newline at end of file