WP_Query::parse_tax_query() – Parses various taxonomy related query vars.

You appear to be a bot. Output may be restricted

Description

Parses various taxonomy related query vars.

For BC, this method is not marked as protected. See [28987].

Usage

WP_Query::parse_tax_query( $q );

Parameters

$q
( array ) required – The query variables. Passed by reference.

Returns

void

Source

File name: wordpress/wp-includes/class-wp-query.php
Lines:

1 to 100 of 231
  public function parse_tax_query( &$q ) {
    if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
      $tax_query = $q['tax_query'];
    } else {
      $tax_query = array();
    }

    if ( ! empty( $q['taxonomy'] ) && ! empty( $q['term'] ) ) {
      $tax_query[] = array(
        'taxonomy' => $q['taxonomy'],
        'terms'    => array( $q['term'] ),
        'field'    => 'slug',
      );
    }

    foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy => $t ) {
      if ( 'post_tag' === $taxonomy ) {
        continue; // Handled further down in the $q['tag'] block.
      }

      if ( $t->query_var && ! empty( $q[ $t->query_var ] ) ) {
        $tax_query_defaults = array(
          'taxonomy' => $taxonomy,
          'field'    => 'slug',
        );

        if ( ! empty( $t->rewrite['hierarchical'] ) ) {
          $q[ $t->query_var ] = wp_basename( $q[ $t->query_var ] );
        }

        $term = $q[ $t->query_var ];

        if ( is_array( $term ) ) {
          $term = implode( ',', $term );
        }

        if ( strpos( $term, '+' ) !== false ) {
          $terms = preg_split( '/[+]+/', $term );
          foreach ( $terms as $term ) {
            $tax_query[] = array_merge(
              $tax_query_defaults,
              array(
                'terms' => array( $term ),
              )
            );
          }
        } else {
          $tax_query[] = array_merge(
            $tax_query_defaults,
            array(
              'terms' => preg_split( '/[,]+/', $term ),
            )
          );
        }
      }
    }

    // If query string 'cat' is an array, implode it.
    if ( is_array( $q['cat'] ) ) {
      $q['cat'] = implode( ',', $q['cat'] );
    }

    // Category stuff.

    if ( ! empty( $q['cat'] ) && ! $this->WP_Query::is_singular ) {
      $cat_in     = array();
      $cat_not_in = array();

      $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) );
      $cat_array = array_map( 'intval', $cat_array );
      $q['cat']  = implode( ',', $cat_array );

      foreach ( $cat_array as $cat ) {
        if ( $cat > 0 ) {
          $cat_in[] = $cat;
        } elseif ( $cat < 0 ) {
          $cat_not_in[] = abs( $cat );
        }
      }

      if ( ! empty( $cat_in ) ) {
        $tax_query[] = array(
          'taxonomy'         => 'category',
          'terms'            => $cat_in,
          'field'            => 'term_id',
          'include_children' => true,
        );
      }

      if ( ! empty( $cat_not_in ) ) {
        $tax_query[] = array(
          'taxonomy'         => 'category',
          'terms'            => $cat_not_in,
          'field'            => 'term_id',
          'operator'         => 'NOT IN',
          'include_children' => true,
        );
      }
      unset( $cat_array, $cat_in, $cat_not_in );
    }
 

 View on GitHub View on Trac