Use taxonomy instead of post2cat. see #4189

git-svn-id: https://develop.svn.wordpress.org/trunk@5592 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2007-05-29 16:52:31 +00:00
parent 1a08c04371
commit e0bf0ffa02
4 changed files with 21 additions and 30 deletions

View File

@ -359,11 +359,11 @@ function get_previous_post($in_same_cat = false, $excluded_categories = '') {
$join = '';
if ( $in_same_cat ) {
$join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
$cat_array = get_the_category($post->ID);
$join .= ' AND (category_id = ' . intval($cat_array[0]->term_id);
$join = " INNER JOIN $wpdb->term_relationships AS tr ON $wpdb->posts.ID = tr.object_id ";
$cat_array = get_object_terms($post->ID, 'category', 'fields=tt_ids');
$join .= ' AND (tr.term_taxonomy_id = ' . intval($cat_array[0]);
for ( $i = 1; $i < (count($cat_array)); $i++ ) {
$join .= ' OR category_id = ' . intval($cat_array[$i]->term_id);
$join .= ' OR tr.term_taxonomy_id = ' . intval($cat_array[$i]);
}
$join .= ')';
}
@ -371,11 +371,7 @@ function get_previous_post($in_same_cat = false, $excluded_categories = '') {
$sql_exclude_cats = '';
if ( !empty($excluded_categories) ) {
$blah = explode(' and ', $excluded_categories);
foreach ( $blah as $category ) {
$category = intval($category);
$sql_cat_ids = " OR pc.category_ID = '$category'";
}
$posts_in_ex_cats = $wpdb->get_col("SELECT p.ID FROM $wpdb->posts p LEFT JOIN $wpdb->post2cat pc ON pc.post_id=p.ID WHERE 1 = 0 $sql_cat_ids GROUP BY p.ID");
$posts_in_ex_cats = get_objects_in_term($blah, 'category');
$posts_in_ex_cats_sql = 'AND ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')';
}
@ -396,11 +392,11 @@ function get_next_post($in_same_cat = false, $excluded_categories = '') {
$join = '';
if ( $in_same_cat ) {
$join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
$cat_array = get_the_category($post->ID);
$join .= ' AND (category_id = ' . intval($cat_array[0]->term_id);
$join = " INNER JOIN $wpdb->term_relationships AS tr ON $wpdb->posts.ID = tr.object_id ";
$cat_array = get_object_terms($post->ID, 'category', 'fields=tt_ids');
$join .= ' AND (tr.term_taxonomy_id = ' . intval($cat_array[0]);
for ( $i = 1; $i < (count($cat_array)); $i++ ) {
$join .= ' OR category_id = ' . intval($cat_array[$i]->term_id);
$join .= ' OR tr.term_taxonomy_id = ' . intval($cat_array[$i]);
}
$join .= ')';
}
@ -408,11 +404,7 @@ function get_next_post($in_same_cat = false, $excluded_categories = '') {
$sql_exclude_cats = '';
if ( !empty($excluded_categories) ) {
$blah = explode(' and ', $excluded_categories);
foreach ( $blah as $category ) {
$category = intval($category);
$sql_cat_ids = " OR pc.category_ID = '$category'";
}
$posts_in_ex_cats = $wpdb->get_col("SELECT p.ID from $wpdb->posts p LEFT JOIN $wpdb->post2cat pc ON pc.post_id = p.ID WHERE 1 = 0 $sql_cat_ids GROUP BY p.ID");
$posts_in_ex_cats = get_objects_in_term($blah, 'category');
$posts_in_ex_cats_sql = 'AND ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')';
}

View File

@ -230,13 +230,13 @@ function get_posts($args) {
$exclusions .= ')';
$query = "SELECT DISTINCT * FROM $wpdb->posts ";
$query .= empty( $category ) ? '' : ", $wpdb->post2cat ";
$query .= empty( $category ) ? '' : ", $wpdb->term_relationships, $wpdb->term_taxonomy ";
$query .= empty( $meta_key ) ? '' : ", $wpdb->postmeta ";
$query .= " WHERE 1=1 ";
$query .= empty( $post_type ) ? '' : "AND post_type = '$post_type' ";
$query .= empty( $post_status ) ? '' : "AND post_status = '$post_status' ";
$query .= "$exclusions $inclusions " ;
$query .= empty( $category ) ? '' : "AND ($wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. ") ";
$query .= empty( $category ) ? '' : "AND ($wpdb->posts.ID = $wpdb->term_relationships.object_id AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id = " . $category. ") ";
$query .= empty( $post_parent ) ? '' : "AND $wpdb->posts.post_parent = '$post_parent' ";
$query .= empty( $meta_key ) | empty($meta_value) ? '' : " AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '$meta_key' AND $wpdb->postmeta.meta_value = '$meta_value' )";
$query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . ' ' . $order;
@ -1695,7 +1695,7 @@ function clean_page_cache($id) {
}
function update_post_category_cache($post_ids) {
global $wpdb, $category_cache, $tag_cache, $blog_id;
global $wpdb, $term_cache, $blog_id;
// TODO
return;
if ( empty($post_ids) )
@ -1708,30 +1708,26 @@ function update_post_category_cache($post_ids) {
$count = count( $post_id_array);
for ( $i = 0; $i < $count; $i++ ) {
$post_id = (int) $post_id_array[ $i ];
if ( isset( $category_cache[$blog_id][$post_id] ) ) {
if ( isset( $term_cache[$blog_id][$post_id] ) ) {
unset( $post_id_array[ $i ] );
continue;
}
}
if ( count( $post_id_array ) == 0 )
return;
$post_id_list = join( ',', $post_id_array ); // with already cached stuff removed
$dogs = $wpdb->get_results("SELECT post_id, category_id, rel_type FROM $wpdb->post2cat WHERE post_id IN ($post_id_list)");
$dogs = get_object_terms($post_id_array, array('category', 'post_tag'));
if ( empty($dogs) )
return;
foreach ($dogs as $catt) {
if ( 'category' == $catt->rel_type )
$category_cache[$blog_id][$catt->post_id][$catt->category_id] = &get_category($catt->category_id);
elseif ( 'tag' == $catt->rel_type )
$tag_cache[$blog_id][$catt->post_id][$catt->category_id] = &get_category($catt->category_id);
$term_cache[$blog_id][$catt->post_id][$catt->taxonomy][$catt->category_id] = &get_category($catt->category_id);
}
}
function update_post_caches(&$posts) {
global $post_cache, $category_cache, $post_meta_cache, $tag_cache;
global $post_cache;
global $wpdb, $blog_id;
// No point in doing all this work if we didn't match any posts.

View File

@ -849,6 +849,7 @@ class WP_Query {
$cat = intval($cat);
$in = (strpos($cat, '-') !== false) ? false : true;
$cat = trim($cat, '-');
// TODO make an array, not a string, for out_cats. use get_term_children()
if ( $in )
$in_cats .= "$cat, " . get_category_children($cat, '', ', ');
else
@ -860,7 +861,7 @@ class WP_Query {
$in_cats = " AND $wpdb->term_taxonomy.term_id IN ({$q['cat']}) ";
if ( strlen($out_cats) > 0 ) {
// TODO use get_objects_in_term
$ids = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE $wpdb->post2cat.category_id IN ($out_cats)");
$ids = get_objects_in_terms($out_cats, 'category');
if ( is_array($ids) && count($ids > 0) ) {
foreach ( $ids as $id )
$out_posts .= "$id, ";

View File

@ -379,6 +379,8 @@ function get_objects_in_term( $terms, $taxonomies, $args = array() ) {
$args = wp_parse_args( $args, $defaults );
extract($args);
$terms = array_map('intval', $terms);
$taxonomies = "'" . implode("', '", $taxonomies) . "'";
$terms = "'" . implode("', '", $terms) . "'";