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 | | |
| 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']); |
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']); |
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 | } |
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) )"; |
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 |