Use taxonomy instead of post2cat. see #4189
git-svn-id: http://svn.automattic.com/wordpress/trunk@5592 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
b0ffd98c95
commit
6a314d85d8
|
@ -359,11 +359,11 @@ function get_previous_post($in_same_cat = false, $excluded_categories = '') {
|
||||||
|
|
||||||
$join = '';
|
$join = '';
|
||||||
if ( $in_same_cat ) {
|
if ( $in_same_cat ) {
|
||||||
$join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
|
$join = " INNER JOIN $wpdb->term_relationships AS tr ON $wpdb->posts.ID = tr.object_id ";
|
||||||
$cat_array = get_the_category($post->ID);
|
$cat_array = get_object_terms($post->ID, 'category', 'fields=tt_ids');
|
||||||
$join .= ' AND (category_id = ' . intval($cat_array[0]->term_id);
|
$join .= ' AND (tr.term_taxonomy_id = ' . intval($cat_array[0]);
|
||||||
for ( $i = 1; $i < (count($cat_array)); $i++ ) {
|
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 .= ')';
|
$join .= ')';
|
||||||
}
|
}
|
||||||
|
@ -371,11 +371,7 @@ function get_previous_post($in_same_cat = false, $excluded_categories = '') {
|
||||||
$sql_exclude_cats = '';
|
$sql_exclude_cats = '';
|
||||||
if ( !empty($excluded_categories) ) {
|
if ( !empty($excluded_categories) ) {
|
||||||
$blah = explode(' and ', $excluded_categories);
|
$blah = explode(' and ', $excluded_categories);
|
||||||
foreach ( $blah as $category ) {
|
$posts_in_ex_cats = get_objects_in_term($blah, '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_sql = 'AND ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')';
|
$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 = '';
|
$join = '';
|
||||||
if ( $in_same_cat ) {
|
if ( $in_same_cat ) {
|
||||||
$join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
|
$join = " INNER JOIN $wpdb->term_relationships AS tr ON $wpdb->posts.ID = tr.object_id ";
|
||||||
$cat_array = get_the_category($post->ID);
|
$cat_array = get_object_terms($post->ID, 'category', 'fields=tt_ids');
|
||||||
$join .= ' AND (category_id = ' . intval($cat_array[0]->term_id);
|
$join .= ' AND (tr.term_taxonomy_id = ' . intval($cat_array[0]);
|
||||||
for ( $i = 1; $i < (count($cat_array)); $i++ ) {
|
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 .= ')';
|
$join .= ')';
|
||||||
}
|
}
|
||||||
|
@ -408,11 +404,7 @@ function get_next_post($in_same_cat = false, $excluded_categories = '') {
|
||||||
$sql_exclude_cats = '';
|
$sql_exclude_cats = '';
|
||||||
if ( !empty($excluded_categories) ) {
|
if ( !empty($excluded_categories) ) {
|
||||||
$blah = explode(' and ', $excluded_categories);
|
$blah = explode(' and ', $excluded_categories);
|
||||||
foreach ( $blah as $category ) {
|
$posts_in_ex_cats = get_objects_in_term($blah, '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_sql = 'AND ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')';
|
$posts_in_ex_cats_sql = 'AND ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -230,13 +230,13 @@ function get_posts($args) {
|
||||||
$exclusions .= ')';
|
$exclusions .= ')';
|
||||||
|
|
||||||
$query = "SELECT DISTINCT * FROM $wpdb->posts ";
|
$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 .= empty( $meta_key ) ? '' : ", $wpdb->postmeta ";
|
||||||
$query .= " WHERE 1=1 ";
|
$query .= " WHERE 1=1 ";
|
||||||
$query .= empty( $post_type ) ? '' : "AND post_type = '$post_type' ";
|
$query .= empty( $post_type ) ? '' : "AND post_type = '$post_type' ";
|
||||||
$query .= empty( $post_status ) ? '' : "AND post_status = '$post_status' ";
|
$query .= empty( $post_status ) ? '' : "AND post_status = '$post_status' ";
|
||||||
$query .= "$exclusions $inclusions " ;
|
$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( $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 .= 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;
|
$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) {
|
function update_post_category_cache($post_ids) {
|
||||||
global $wpdb, $category_cache, $tag_cache, $blog_id;
|
global $wpdb, $term_cache, $blog_id;
|
||||||
// TODO
|
// TODO
|
||||||
return;
|
return;
|
||||||
if ( empty($post_ids) )
|
if ( empty($post_ids) )
|
||||||
|
@ -1708,30 +1708,26 @@ function update_post_category_cache($post_ids) {
|
||||||
$count = count( $post_id_array);
|
$count = count( $post_id_array);
|
||||||
for ( $i = 0; $i < $count; $i++ ) {
|
for ( $i = 0; $i < $count; $i++ ) {
|
||||||
$post_id = (int) $post_id_array[ $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 ] );
|
unset( $post_id_array[ $i ] );
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( count( $post_id_array ) == 0 )
|
if ( count( $post_id_array ) == 0 )
|
||||||
return;
|
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) )
|
if ( empty($dogs) )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
foreach ($dogs as $catt) {
|
foreach ($dogs as $catt) {
|
||||||
if ( 'category' == $catt->rel_type )
|
$term_cache[$blog_id][$catt->post_id][$catt->taxonomy][$catt->category_id] = &get_category($catt->category_id);
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function update_post_caches(&$posts) {
|
function update_post_caches(&$posts) {
|
||||||
global $post_cache, $category_cache, $post_meta_cache, $tag_cache;
|
global $post_cache;
|
||||||
global $wpdb, $blog_id;
|
global $wpdb, $blog_id;
|
||||||
|
|
||||||
// No point in doing all this work if we didn't match any posts.
|
// No point in doing all this work if we didn't match any posts.
|
||||||
|
|
|
@ -849,6 +849,7 @@ class WP_Query {
|
||||||
$cat = intval($cat);
|
$cat = intval($cat);
|
||||||
$in = (strpos($cat, '-') !== false) ? false : true;
|
$in = (strpos($cat, '-') !== false) ? false : true;
|
||||||
$cat = trim($cat, '-');
|
$cat = trim($cat, '-');
|
||||||
|
// TODO make an array, not a string, for out_cats. use get_term_children()
|
||||||
if ( $in )
|
if ( $in )
|
||||||
$in_cats .= "$cat, " . get_category_children($cat, '', ', ');
|
$in_cats .= "$cat, " . get_category_children($cat, '', ', ');
|
||||||
else
|
else
|
||||||
|
@ -860,7 +861,7 @@ class WP_Query {
|
||||||
$in_cats = " AND $wpdb->term_taxonomy.term_id IN ({$q['cat']}) ";
|
$in_cats = " AND $wpdb->term_taxonomy.term_id IN ({$q['cat']}) ";
|
||||||
if ( strlen($out_cats) > 0 ) {
|
if ( strlen($out_cats) > 0 ) {
|
||||||
// TODO use get_objects_in_term
|
// 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) ) {
|
if ( is_array($ids) && count($ids > 0) ) {
|
||||||
foreach ( $ids as $id )
|
foreach ( $ids as $id )
|
||||||
$out_posts .= "$id, ";
|
$out_posts .= "$id, ";
|
||||||
|
|
|
@ -379,6 +379,8 @@ function get_objects_in_term( $terms, $taxonomies, $args = array() ) {
|
||||||
$args = wp_parse_args( $args, $defaults );
|
$args = wp_parse_args( $args, $defaults );
|
||||||
extract($args);
|
extract($args);
|
||||||
|
|
||||||
|
$terms = array_map('intval', $terms);
|
||||||
|
|
||||||
$taxonomies = "'" . implode("', '", $taxonomies) . "'";
|
$taxonomies = "'" . implode("', '", $taxonomies) . "'";
|
||||||
$terms = "'" . implode("', '", $terms) . "'";
|
$terms = "'" . implode("', '", $terms) . "'";
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue