WordPress.org

Make WordPress Core

Ticket #16157: 16157.2.diff

File 16157.2.diff, 3.6 KB (added by ryan, 2 years ago)

Support AND operator

  • wp-includes/taxonomy.php

     
    671671                                $join .= " ON ($primary_table.$primary_id_column = $alias.object_id)"; 
    672672 
    673673                                $where[] = "$alias.term_taxonomy_id $operator ($terms)"; 
    674                         } 
    675                         elseif ( 'NOT IN' == $operator ) { 
     674                        } elseif ( 'NOT IN' == $operator ) { 
    676675 
    677676                                if ( empty( $terms ) ) 
    678677                                        continue; 
     
    684683                                        FROM $wpdb->term_relationships 
    685684                                        WHERE term_taxonomy_id IN ($terms) 
    686685                                )"; 
     686                        } elseif ( 'AND' == $operator ) { 
     687 
     688                                if ( empty( $terms ) ) 
     689                                        continue; 
     690 
     691                                $num_terms = count( $terms ); 
     692 
     693                                $terms = implode( ',', $terms ); 
     694 
     695                                $where[] = "$primary_table.$primary_id_column IN ( 
     696                                        SELECT object_id 
     697                                        FROM $wpdb->term_relationships 
     698                                        WHERE term_taxonomy_id IN ($terms) 
     699                                        GROUP BY object_id HAVING COUNT(object_id) = $num_terms 
     700                                )"; 
    687701                        } 
    688702 
    689703                        $i++; 
  • wp-includes/query.php

     
    16921692                } 
    16931693 
    16941694                if ( !empty($q['category__in']) ) { 
    1695                         $q['category__in'] = array_unique( $q['category__in'] ); 
     1695                        $q['category__in'] = array_map('absint', array_unique( $q['category__in'] ) ); 
    16961696                        $tax_query[] = array( 
    16971697                                'taxonomy' => 'category', 
    16981698                                'terms' => $q['category__in'], 
     
    17011701                } 
    17021702 
    17031703                if ( !empty($q['category__not_in']) ) { 
    1704                         $q['category__not_in'] = array_unique( $q['category__not_in'] ); 
     1704                        $q['category__not_in'] = array_map('absint', array_unique( $q['category__not_in'] ) ); 
    17051705                        $tax_query[] = array( 
    17061706                                'taxonomy' => 'category', 
    17071707                                'terms' => $q['category__not_in'], 
    1708                                 'operator' => 'NOT IN', 
     1708                                'operator' => 'NOT IN' 
    17091709                        ); 
    17101710                } 
    17111711 
     1712                if ( !empty($q['category__and']) ) { 
     1713                        $q['category__and'] = array_map('absint', array_unique( $q['category__and'] ) ); 
     1714                        $tax_query[] = array( 
     1715                                'taxonomy' => 'category', 
     1716                                'terms' => $q['category__and'], 
     1717                                'field' => 'term_id', 
     1718                                'operator' => 'AND' 
     1719                        ); 
     1720                } 
     1721 
    17121722                // Tag stuff 
    17131723                if ( !empty($q['tag_id']) ) { 
     1724                        $q['tag_id'] = absint( $q['tag_id'] ); 
    17141725                        $tax_query[] = array( 
    17151726                                'taxonomy' => 'post_tag', 
    1716                                 'terms' => $q['tag_id'], 
     1727                                'terms' => $q['tag_id'] 
    17171728                        ); 
    17181729                } 
    17191730 
    17201731                if ( !empty($q['tag__in']) ) { 
     1732                        $q['tag__in'] = array_map('absint', array_unique( $q['tag__in'] ) ); 
    17211733                        $tax_query[] = array( 
    17221734                                'taxonomy' => 'post_tag', 
    1723                                 'terms' => $q['tag__in'], 
     1735                                'terms' => $q['tag__in'] 
    17241736                        ); 
    17251737                } 
    17261738 
    17271739                if ( !empty($q['tag__not_in']) ) { 
     1740                        $q['tag__not_in'] = array_map('absint', array_unique( $q['tag__not_in'] ) ); 
    17281741                        $tax_query[] = array( 
    17291742                                'taxonomy' => 'post_tag', 
    17301743                                'terms' => $q['tag__not_in'], 
    1731                                 'operator' => 'NOT IN', 
     1744                                'operator' => 'NOT IN' 
    17321745                        ); 
    17331746                } 
    17341747 
     1748                if ( !empty($q['tag__and']) ) { 
     1749                        $q['tag__and'] = array_map('absint', array_unique( $q['tag__and'] ) ); 
     1750                        $tax_query[] = array( 
     1751                                'taxonomy' => 'post_tag', 
     1752                                'terms' => $q['tag__and'], 
     1753                                'operator' => 'AND' 
     1754                        ); 
     1755                } 
     1756 
     1757                if ( !empty($q['tag_slug__in']) ) { 
     1758                        $q['tag_slug__in'] = array_map('sanitize_title', $q['tag_slug__in']); 
     1759                        $tax_query[] = array( 
     1760                                'taxonomy' => 'post_tag', 
     1761                                'terms' => $q['tag_slug__in'], 
     1762                                'field' => 'slug' 
     1763                        ); 
     1764                } 
     1765 
     1766                if ( !empty($q['tag_slug__and']) ) { 
     1767                        $q['tag_slug__and'] = array_map('sanitize_title', $q['tag_slug__and']); 
     1768                        $tax_query[] = array( 
     1769                                'taxonomy' => 'post_tag', 
     1770                                'terms' => $q['tag_slug__and'], 
     1771                                'field' => 'slug', 
     1772                                'operator' => 'AND' 
     1773                        ); 
     1774                } 
     1775 
    17351776                $this->tax_query = new WP_Tax_Query( $tax_query ); 
    17361777        } 
    17371778