Make WordPress Core

Ticket #9951: query.diff

File query.diff, 4.1 KB (added by joehoyle, 16 years ago)

Fixed problem with diff, sorry about that - not too used to all this svn crazyness!

  • query.php

     
    13951395                                $qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']);
    13961396                                $this->is_tag = true;
    13971397                        }
     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                        }
    13981417
    13991418                        if ( empty($qv['taxonomy']) || empty($qv['term']) ) {
    14001419                                $this->is_tax = false;
     
    19011920                                        $whichcat .= " AND $wpdb->posts.ID NOT IN ('" . implode("', '", $ids) . "')";
    19021921                        }
    19031922                }
     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                }
    19041942
    1905                 // Tag and slug intersections.
     1943                // Tag and slug intersections. Also merge with custom taxonomies
    19061944                $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 );
    19071946                $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below
    19081947                foreach ($intersections as $item => $taxonomy) {
    19091948                        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                                               
    19121950                        if ( $item != 'category__and' ) {
    19131951                                $reqtag = is_term( $q[$item][0], 'post_tag' );
    19141952                                if ( !empty($reqtag) )
    19151953                                        $q['tag_id'] = $reqtag['term_id'];
    19161954                        }
    19171955
    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';
    19221957
    19231958                        $q[$item] = array_unique($q[$item]);
    19241959                        $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]) . "')";                   
    19261961                        if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship
    19271962                                $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
    19281963                        }
     
    26972732        return true;
    26982733}
    26992734
    2700 ?>
     2735?>
     2736 No newline at end of file