Make WordPress Core


Ignore:
Timestamp:
10/15/2014 12:53:22 AM (10 years ago)
Author:
boonebgorges
Message:

Support 'EXISTS' and 'NOT EXISTS' in WP_Tax_Query.

These new values for the 'operator' parameter make it possible to filter items
that have no term from a given taxonomy, or any term from a given taxonomy.

Includes unit tests.

Fixes #29181.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r29891 r29896  
    692692     *
    693693     * @since 3.1.0
     694     * @since 4.1.0 Added support for $operator 'NOT EXISTS' and 'EXISTS'.
    694695     * @access public
    695696     *
    696697     * @param array $tax_query {
    697      *     Array of taxonoy query clauses.
     698     *     Array of taxonomy query clauses.
    698699     *
    699700     *     @type string $relation Optional. The MySQL keyword used to join
     
    707708     *                                                 'name', or 'term_taxonomy_id'. Default: 'term_id'.
    708709     *         @type string           $operator         MySQL operator to be used with $terms in the WHERE clause.
    709      *                                                  Accepts 'AND', 'IN', or 'OR. Default: 'IN'.
     710     *                                                  Accepts 'AND', 'IN', 'NOT IN', 'EXISTS', 'NOT EXISTS'.
     711     *                                                  Default: 'IN'.
    710712     *         @type bool             $include_children Optional. Whether to include child terms.
    711713     *                                                  Requires a $taxonomy. Default: true.
     
    10271029                AND object_id = $this->primary_table.$this->primary_id_column
    10281030            ) = $num_terms";
     1031
     1032        } elseif ( 'NOT EXISTS' === $operator || 'EXISTS' === $operator ) {
     1033
     1034            $where = $wpdb->prepare( "$operator (
     1035                SELECT 1
     1036                FROM $wpdb->term_relationships
     1037                INNER JOIN $wpdb->term_taxonomy
     1038                ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
     1039                WHERE $wpdb->term_taxonomy.taxonomy = %s
     1040                AND $wpdb->term_relationships.object_id = $this->primary_table.$this->primary_id_column
     1041            )", $clause['taxonomy'] );
     1042
    10291043        }
    10301044
Note: See TracChangeset for help on using the changeset viewer.