Ticket #20597: comment-query-by-category.patch
File comment-query-by-category.patch, 2.8 KB (added by , 13 years ago) |
---|
-
wp-includes/comment.php
222 222 'type' => '', 223 223 'user_id' => '', 224 224 'search' => '', 225 'count' => false 225 'count' => false, 226 'cat' => false, 227 'category__in' => array(), 228 'category__not_in' => array() 226 229 ); 227 230 228 231 $this->query_vars = wp_parse_args( $query_vars, $defaults ); … … 301 304 else 302 305 $fields = '*'; 303 306 307 if ( empty( $category__in ) ) { 308 $category__in = array(); 309 } 310 if ( empty( $category__not_in ) ) { 311 $category__not_in = array(); 312 } 313 314 if ( !empty($cat) && '0' != $cat ) { 315 $cat = ''.urldecode($cat).''; 316 $cat = addslashes_gpc($cat); 317 $cat_array = preg_split('/[,\s]+/', $cat); 318 $cat = ''; 319 $req_cats = array(); 320 foreach ( (array) $cat_array as $cat_id ) { 321 $cat_id = intval($cat_id); 322 $req_cats[] = $cat_id; 323 $in = ($cat_id > 0); 324 $cat_id = abs($cat_id); 325 if ( $in ) { 326 $category__in[] = $cat_id; 327 $category__in = array_merge( $category__in, get_term_children($cat_id, 'category') ); 328 } else { 329 $category__not_in[] = $cat_id; 330 $category__not_in = array_merge( $category__not_in, get_term_children($cat_id, 'category') ); 331 } 332 } 333 $cat = implode(',', $req_cats); 334 } 335 304 336 $join = ''; 305 337 $where = $approved; 306 338 … … 330 362 foreach( $post_fields as $field_name => $field_value ) 331 363 $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value ); 332 364 } 365 if ( ! empty( $category__in ) || ! empty( $category__not_in ) ) { 366 if ( empty( $join ) ) { 367 $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; 368 } 369 $join .= " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)"; 370 $join .= " INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)"; 371 $where .= " AND $wpdb->term_taxonomy.taxonomy = 'category'"; 333 372 373 if ( ! empty( $category__in ) ) { 374 $category__in = array_map( 'absint', array_unique( (array) $category__in ) ); 375 $category__in_query = implode( ',', $category__in ); 376 $where .= $wpdb->prepare( " AND $wpdb->term_taxonomy.term_id IN ($category__in_query)" ); 377 } 378 379 if ( ! empty( $category__not_in ) ) { 380 $category__not_in = array_map( 'absint', array_unique( (array) $category__not_in ) ); 381 $category__not_in_query = implode( ',', $category__not_in ); 382 $where .= $wpdb->prepare( " AND $wpdb->term_taxonomy.term_id NOT IN ($category__not_in_query)" ); 383 } 384 } 385 334 386 $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' ); 335 387 $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) ); 336 388 foreach ( $pieces as $piece )