Make WordPress Core


Ignore:
Timestamp:
03/23/2008 05:02:11 PM (17 years ago)
Author:
ryan
Message:

Taxonomy queries and urls. Props andy. see #6357

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/query.php

    r7109 r7491  
    115115
    116116    if ( in_array( $tag_obj->slug, $slug ) )
     117        return true;
     118
     119    return false;
     120}
     121
     122function is_tax( $slug = '' ) {
     123    global $wp_query;
     124   
     125    if ( !$wp_query->is_tax )
     126        return false;
     127
     128    if ( empty($slug) )
     129        return true;
     130
     131    $term = $wp_query->get_queried_object();
     132
     133    $slug = (array) $slug;
     134
     135    if ( in_array( $term->slug, $slug ) )
    117136        return true;
    118137
     
    371390    var $is_category = false;
    372391    var $is_tag = false;
     392    var $is_tax = false;
    373393    var $is_search = false;
    374394    var $is_feed = false;
     
    396416        $this->is_category = false;
    397417        $this->is_tag = false;
     418        $this->is_tax = false;
    398419        $this->is_search = false;
    399420        $this->is_feed = false;
     
    658679            }
    659680
     681            if ( empty($qv['taxonomy']) || empty($qv['term']) ) {
     682                $this->is_tax = false;
     683            } else {
     684                $this->is_tax = true;
     685            }
     686
    660687            if ( empty($qv['author']) || ($qv['author'] == '0') ) {
    661688                $this->is_author = false;
     
    841868        if ( $q['m'] ) {
    842869            $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
    843             $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4);
     870            $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
    844871            if (strlen($q['m'])>5)
    845                 $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2);
     872                $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
    846873            if (strlen($q['m'])>7)
    847                 $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2);
     874                $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
    848875            if (strlen($q['m'])>9)
    849                 $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2);
     876                $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
    850877            if (strlen($q['m'])>11)
    851                 $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2);
     878                $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
    852879            if (strlen($q['m'])>13)
    853                 $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2);
     880                $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
    854881        }
    855882
    856883        if ( '' !== $q['hour'] )
    857             $where .= " AND HOUR(post_date)='" . $q['hour'] . "'";
     884            $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
    858885
    859886        if ( '' !== $q['minute'] )
    860             $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'";
     887            $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
    861888
    862889        if ( '' !== $q['second'] )
    863             $where .= " AND SECOND(post_date)='" . $q['second'] . "'";
     890            $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
    864891
    865892        if ( $q['year'] )
    866             $where .= " AND YEAR(post_date)='" . $q['year'] . "'";
     893            $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
    867894
    868895        if ( $q['monthnum'] )
    869             $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'";
     896            $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
    870897
    871898        if ( $q['day'] )
    872             $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'";
     899            $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
    873900
    874901        if ('' != $q['name']) {
    875902            $q['name'] = sanitize_title($q['name']);
    876             $where .= " AND post_name = '" . $q['name'] . "'";
     903            $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
    877904        } else if ('' != $q['pagename']) {
    878905            if ( isset($this->queried_object_id) )
     
    904931            $q['attachment'] = sanitize_title(basename($attach_paths));
    905932            $q['name'] = $q['attachment'];
    906             $where .= " AND post_name = '" . $q['attachment'] . "'";
     933            $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
    907934        }
    908935
    909936        if ( $q['w'] )
    910             $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'";
     937            $where .= " AND WEEK($wpdb->posts.post_date, 1)='" . $q['w'] . "'";
    911938
    912939        if ( intval($q['comments_popup']) )
     
    942969            foreach((array)$q['search_terms'] as $term) {
    943970                $term = addslashes_gpc($term);
    944                 $search .= "{$searchand}((post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}'))";
     971                $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
    945972                $searchand = ' AND ';
    946973            }
    947974            $term = $wpdb->escape($q['s']);
    948975            if (!$q['sentence'] && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
    949                 $search .= " OR (post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}')";
     976                $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
    950977
    951978            if ( !empty($search) )
     
    11131140        }
    11141141
     1142        // Taxonomies
     1143        if ( $this->is_tax ) {
     1144            $terms = get_terms($q['taxonomy'], array('slug'=>$q['term']));
     1145            foreach ( $terms as $term )
     1146                $term_ids[] = $term->term_id;
     1147            $post_ids = get_objects_in_term($term_ids, $q['taxonomy']);
     1148
     1149            if ( count($post_ids) ) {
     1150                $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
     1151                $post_type = 'any';
     1152                $q['post_status'] = 'publish';
     1153                $post_status_join = true;
     1154            } else {
     1155                $whichcat = " AND 0 = 1";
     1156            }
     1157        }
     1158
    11151159        // Author/user stuff
    11161160
     
    11301174            }
    11311175            $author_array = preg_split('/[,\s]+/', $q['author']);
    1132             $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]);
     1176            $whichauthor .= " AND ($wpdb->posts.post_author ".$eq.' '.intval($author_array[0]);
    11331177            for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
    1134                 $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
     1178                $whichauthor .= ' '.$andor." $wpdb->posts.post_author ".$eq.' '.intval($author_array[$i]);
    11351179            }
    11361180            $whichauthor .= ')';
     
    11501194            $q['author_name'] = sanitize_title($q['author_name']);
    11511195            $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'");
    1152             $whichauthor .= ' AND (post_author = '.intval($q['author']).')';
     1196            $whichauthor .= " AND ($wpdb->posts.post_author = ".intval($q['author']).')';
    11531197        }
    11541198
     
    11651209        // Order by
    11661210        if ( empty($q['orderby']) ) {
    1167             $q['orderby'] = 'post_date '.$q['order'];
     1211            $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
    11681212        } else {
    11691213            // Used to filter values
     
    11811225                    case 'menu_order':
    11821226                    case 'ID':
     1227                        $orderby = "$wpdb->posts.ID";
    11831228                        break;
    11841229                    case 'rand':
     
    11861231                        break;
    11871232                    default:
    1188                         $orderby = 'post_' . $orderby;
     1233                        $orderby = "$wpdb->posts.post_" . $orderby;
    11891234                }
    11901235                if ( in_array($orderby_array[$i], $allowed_keys) )
     
    11961241
    11971242            if ( empty($q['orderby']) )
    1198                 $q['orderby'] = 'post_date '.$q['order'];
     1243                $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
    11991244        }
    12001245
    12011246        if ( $this->is_attachment ) {
    1202             $where .= " AND post_type = 'attachment'";
     1247            $where .= " AND $wpdb->posts.post_type = 'attachment'";
    12031248        } elseif ($this->is_page) {
    1204             $where .= " AND post_type = 'page'";
     1249            $where .= " AND $wpdb->posts.post_type = 'page'";
    12051250        } elseif ($this->is_single) {
    1206             $where .= " AND post_type = 'post'";
     1251            $where .= " AND $wpdb->posts.post_type = 'post'";
    12071252        } elseif ( 'any' == $post_type ) {
    12081253            $where .= '';
    12091254        } else {
    1210             $where .= " AND post_type = '$post_type'";
     1255            $where .= " AND $wpdb->posts.post_type = '$post_type'";
    12111256        }
    12121257
    12131258        if ( isset($q['post_status']) && '' != $q['post_status'] ) {
     1259            $statuswheres = array();
    12141260            $q_status = explode(',', $q['post_status']);
    12151261            $r_status = array();
    12161262            $p_status = array();
    12171263            if ( in_array( 'draft'  , $q_status ) )
    1218                 $r_status[] = "post_status = 'draft'";
     1264                $r_status[] = "$wpdb->posts.post_status = 'draft'";
    12191265            if ( in_array( 'pending', $q_status ) )
    1220                 $r_status[] = "post_status = 'pending'";
     1266                $r_status[] = "$wpdb->posts.post_status = 'pending'";
    12211267            if ( in_array( 'future' , $q_status ) )
    1222                 $r_status[] = "post_status = 'future'";
     1268                $r_status[] = "$wpdb->posts.post_status = 'future'";
    12231269            if ( in_array( 'inherit' , $q_status ) )
    1224                 $r_status[] = "post_status = 'inherit'";
     1270                $r_status[] = "$wpdb->posts.post_status = 'inherit'";
    12251271            if ( in_array( 'private', $q_status ) )
    1226                 $p_status[] = "post_status = 'private'";
     1272                $p_status[] = "$wpdb->posts.post_status = 'private'";
    12271273            if ( in_array( 'publish', $q_status ) )
    1228                 $r_status[] = "post_status = 'publish'";
     1274                $r_status[] = "$wpdb->posts.post_status = 'publish'";
    12291275
    12301276            if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
     
    12351281            if ( !empty($r_status) ) {
    12361282                if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can("edit_others_{$post_type}s") )
    1237                     $where .= " AND (post_author = $user_ID " .  "AND (" . join( ' OR ', $r_status ) . "))";
     1283                    $statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $r_status ) . "))";
    12381284                else
    1239                     $where .= " AND (" . join( ' OR ', $r_status ) . ")";
     1285                    $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
    12401286            }
    12411287            if ( !empty($p_status) ) {
    12421288                if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can("read_private_{$post_type}s") )
    1243                     $where .= " AND (post_author = $user_ID " .  "AND (" . join( ' OR ', $p_status ) . "))";
     1289                    $statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $p_status ) . "))";
    12441290                else
    1245                     $where .= " AND (" . join( ' OR ', $p_status ) . ")";
    1246             }
     1291                    $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
     1292            }
     1293            if ( $post_status_join ) {
     1294                $join .= " INNER JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
     1295                foreach ( $statuswheres as $index => $statuswhere )
     1296                    $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
     1297            }
     1298            foreach ( $statuswheres as $statuswhere )
     1299                $where .= " AND $statuswhere";
    12471300        } elseif ( !$this->is_singular ) {
    1248             $where .= " AND (post_status = 'publish'";
     1301            $where .= " AND ($wpdb->posts.post_status = 'publish'";
    12491302
    12501303            if ( is_admin() )
    1251                 $where .= " OR post_status = 'future' OR post_status = 'draft' OR post_status = 'pending'";
     1304                $where .= " OR $wpdb->posts.post_status = 'future' OR $wpdb->posts.post_status = 'draft' OR $wpdb->posts.post_status = 'pending'";
    12521305
    12531306            if ( is_user_logged_in() ) {
    1254                 $where .= current_user_can( "read_private_{$post_type}s" ) ? " OR post_status = 'private'" : " OR post_author = $user_ID AND post_status = 'private'";
     1307                $where .= current_user_can( "read_private_{$post_type}s" ) ? " OR $wpdb->posts.post_status = 'private'" : " OR $wpdb->posts.post_author = $user_ID AND $wpdb->posts.post_status = 'private'";
    12551308            }
    12561309
     
    15051558            $this->queried_object = &$tag;
    15061559            $this->queried_object_id = (int) $tag_id;
     1560        } else if ($this->is_tax) {
     1561            $tax = $this->get('taxonomy');
     1562            $slug = $this->get('term');
     1563            $term = &get_terms($tax, array('slug'=>$slug));
     1564            if ( is_wp_error($term) )
     1565                return $term;
     1566            $this->queried_object = $term;
     1567            $this->queried_object_id = $term->term_id;
    15071568        } else if ($this->is_posts_page) {
    15081569            $this->queried_object = & get_page(get_option('page_for_posts'));
Note: See TracChangeset for help on using the changeset viewer.