Make WordPress Core

Ticket #9951: tax-experimental.diff

File tax-experimental.diff, 8.4 KB (added by joehoyle, 15 years ago)

Added experimental patch which replaces all the category*, tag* to use all registered taxonomies. (not working)

  • query.php

     
    17941794                        $q['cat'] = implode(',', $req_cats);
    17951795                }
    17961796
    1797                 if ( !empty($q['category__in']) ) {
    1798                         $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) ";
    1799                         $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
    1800                         $include_cats = "'" . implode("', '", $q['category__in']) . "'";
    1801                         $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
    1802                 }
    1803 
    1804                 if ( !empty($q['category__not_in']) ) {
    1805                         $cat_string = "'" . implode("', '", $q['category__not_in']) . "'";
    1806                         $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) )";
    1807                 }
    1808 
    18091797                // Category stuff for nice URLs
    18101798                if ( '' != $q['category_name'] && !$this->is_singular ) {
    18111799                        $q['category_name'] = implode('/', array_map('sanitize_title', explode('/', $q['category_name'])));
     
    18601848                                $q['tag_slug__in'][] = $q['tag'];
    18611849                        }
    18621850                }
     1851               
     1852                // Set up the arrays for querying by taxonomy (tag and category included)
     1853                $this->taxonomies__in = array();
     1854                $this->taxonomies__not_in = array();
     1855                $this->taxonomies__and = array();
     1856                $this->taxonomies_slug__in = array();
     1857                $this->taxonomies_slug__and = array();
     1858                       
     1859                // loop through registered taxonomies to check for respective query vars (must be done after 'tag' and 'cat')
     1860                foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
     1861                   
     1862                    // check if query vars have been passed for the taxonomy (annoying explicit check for tag as the taxonomy is called post_tag)
     1863                    if( array_key_exists( $taxonomy . '__in', $q ) && !empty($qv[$taxonomy . '__in']) ) {
     1864                        $is = 'is_' . $taxonomy;
     1865                        $this->$is = true;
     1866                        $this->taxonomies__in[$taxonomy . '__in'] = $taxonomy;
     1867                       
     1868                        // cast the array to integers, again annoying check for tag passed instead of post_tag
     1869                        $q[$taxonomy . '__in'] = array_map('absint', $q[$taxonomy . '__in']);
     1870                    }
     1871                    elseif( array_key_exists( $taxonomy . '__not_in', $q ) && !empty($q[$taxonomy . '__not_in']) ) {
     1872                        $is = 'is_' . $taxonomy;
     1873                        $this->taxonomies__not_in[$taxonomy . '__not_in'] = $taxonomy;
     1874                        $q[$taxonomy . '__not_in'] = array_map('absint', $q[$taxonomy . '__not_in']);
    18631875
    1864                 if ( !empty($q['category__in']) || !empty($q['meta_key']) || !empty($q['tag__in']) || !empty($q['tag_slug__in']) ) {
    1865                         $groupby = "{$wpdb->posts}.ID";
    1866                 }
     1876                    }
     1877                    elseif( array_key_exists( $taxonomy . '__and', $q ) && !empty($q[$taxonomy . '__and']) ) {
     1878                        $is = 'is_' . $taxonomy;
     1879                        $this->$is = true;
     1880                        $this->taxonomies__and[$taxonomy . '__and'] = $taxonomy;
     1881                        $q[$taxonomy . '__and'] = array_map('absint', $q[$taxonomy . '__and']);
     1882                    }
     1883                    elseif( array_key_exists( $taxonomy . '_slug__in', $q ) && !empty($q[$taxonomy . '_slug__in']) ) {
     1884                        $is = 'is_' . $taxonomy;
     1885                        $this->$is = true;
     1886                        $this->taxonomies_slug__in[$taxonomy . '_slug__in'] = $taxonomy;
     1887                        $q[$taxonomy . '_slug__in'] = array_map('sanitize_title', $q[$taxonomy . '_slug__in']);
    18671888
    1868                 if ( !empty($q['tag__in']) && empty($q['cat']) ) {
    1869                         $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) ";
    1870                         $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
    1871                         $include_tags = "'" . implode("', '", $q['tag__in']) . "'";
    1872                         $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";
    1873                         $reqtag = is_term( $q['tag__in'][0], 'post_tag' );
    1874                         if ( !empty($reqtag) )
    1875                                 $q['tag_id'] = $reqtag['term_id'];
     1889                    }
     1890                    elseif( array_key_exists( $taxonomy . '_slug__and', $q ) && !empty($q[$taxonomy . '_slug__and']) ) {
     1891                        $is = 'is_' . $taxonomy;
     1892                        $this->$is = true;
     1893                        $this->taxonomies_slug__and[$taxonomy . '_slug__and'] = $taxonomy;
     1894                        $qv[$taxonomy . '_slug__and'] = array_map('sanitize_title', $q[$taxonomy . '_slug__and']);
     1895                    }
    18761896                }
    18771897
    1878                 if ( !empty($q['tag_slug__in']) && empty($q['cat']) ) {
    1879                         $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) ";
    1880                         $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
    1881                         $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";
    1882                         $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";
    1883                         $reqtag = get_term_by( 'slug', $q['tag_slug__in'][0], 'post_tag' );
    1884                         if ( !empty($reqtag) )
    1885                                 $q['tag_id'] = $reqtag->term_id;
     1898                //Taxonomies not in
     1899                foreach( (array) $this->taxonomies__not_in as $taxonomy ) {
     1900                        $term_string = "'" . implode("', '", $q[$taxonomy . '__not_in']) . "'";
     1901                        $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 = '$taxonomy' AND tt.term_id IN ($term_string) )";
    18861902                }
    18871903
    1888                 if ( !empty($q['tag__not_in']) ) {
    1889                         $tag_string = "'" . implode("', '", $q['tag__not_in']) . "'";
    1890                         $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) )";
    1891                 }
    1892 
    1893                 // Tag and slug intersections.
    1894                 $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag');
    1895                 $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below
     1904                // Term and slug intersections.
     1905                $intersections = array();
     1906                $intersections = array_merge( $intersections, $this->taxonomies__in, $this->taxonomies__and, $this->taxonomies_slug__in, $this->taxonomies_slug__and );
     1907                $intersections = array_merge( $intersections, array('tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag') );
     1908               
     1909                $tagin = array_merge( array_keys( $this->taxonomies__in ), array('tag__in', 'tag_slug__in') ); // These are used to make some exceptions below
    18961910                foreach ($intersections as $item => $taxonomy) {
     1911                       
    18971912                        if ( empty($q[$item]) ) continue;
    1898                         if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used
    1899 
     1913                       
    19001914                        if ( $item != 'category__and' ) {
    19011915                                $reqtag = is_term( $q[$item][0], 'post_tag' );
    19021916                                if ( !empty($reqtag) )
    19031917                                        $q['tag_id'] = $reqtag['term_id'];
    19041918                        }
    19051919
    1906                         if ( in_array( $item, array('tag_slug__and', 'tag_slug__in' ) ) )
     1920                        if ( in_array( $item, array_merge( array_keys( $this->taxonomies_slug__in ), array_keys( $this->taxonomies_slug__and ), array('tag_slug__and', 'tag_slug__in' ) ) ) )
    19071921                                $taxonomy_field = 'slug';
    19081922                        else
    19091923                                $taxonomy_field = 'term_id';
    1910 
    19111924                        $q[$item] = array_unique($q[$item]);
    19121925                        $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)";
    19131926                        $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
     
    21542167                if ( ! empty($q['meta_key']) )
    21552168                        $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']);
    21562169                if ( ! empty($q['meta_value']) ) {
    2157                         if ( ! isset($q['meta_compare']) || empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
     2170                        if ( ! isset($q['meta_compare']) || empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=', 'LIKE')) )
    21582171                                $q['meta_compare'] = '=';
    21592172
    21602173                        $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value {$q['meta_compare']} %s ", $q['meta_value']);
     
    27252738        return true;
    27262739}
    27272740
    2728 ?>
     2741?>
     2742 No newline at end of file