| 417 | | extract( $r, EXTR_SKIP ); |
|---|
| | 415 | if ( ! empty($r['numberposts']) ) |
|---|
| | 416 | $r['posts_per_page'] = $r['numberposts']; |
|---|
| | 417 | if ( ! empty($r['category']) ) |
|---|
| | 418 | $r['cat'] = $r['category']; |
|---|
| | 419 | if ( ! empty($r['include']) ) { |
|---|
| | 420 | $incposts = preg_split('/[\s,]+/',$r['include']); |
|---|
| | 421 | $r['posts_per_page'] = count($incposts); // only the number of posts included |
|---|
| | 422 | $r['post__in'] = $incposts; |
|---|
| | 423 | } elseif ( ! empty($r['exclude']) ) |
|---|
| | 424 | $r['post__not_in'] = preg_split('/[\s,]+/',$r['exclude']); |
|---|
| 424 | | $inclusions = ''; |
|---|
| 425 | | if ( !empty($include) ) { |
|---|
| 426 | | $offset = 0; //ignore offset, category, exclude, meta_key, and meta_value, post_parent if using include |
|---|
| 427 | | $category = 0; |
|---|
| 428 | | $exclude = ''; |
|---|
| 429 | | $meta_key = ''; |
|---|
| 430 | | $meta_value = ''; |
|---|
| 431 | | $post_parent = 0; |
|---|
| 432 | | $incposts = preg_split('/[\s,]+/',$include); |
|---|
| 433 | | $numberposts = count($incposts); // only the number of posts included |
|---|
| 434 | | if ( count($incposts) ) { |
|---|
| 435 | | foreach ( $incposts as $incpost ) { |
|---|
| 436 | | if (empty($inclusions)) |
|---|
| 437 | | $inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpost); |
|---|
| 438 | | else |
|---|
| 439 | | $inclusions .= $wpdb->prepare(' OR ID = %d ', $incpost); |
|---|
| 440 | | } |
|---|
| 441 | | } |
|---|
| 442 | | } |
|---|
| 443 | | if (!empty($inclusions)) |
|---|
| 444 | | $inclusions .= ')'; |
|---|
| 445 | | |
|---|
| 446 | | $exclusions = ''; |
|---|
| 447 | | if ( !empty($exclude) ) { |
|---|
| 448 | | $exposts = preg_split('/[\s,]+/',$exclude); |
|---|
| 449 | | if ( count($exposts) ) { |
|---|
| 450 | | foreach ( $exposts as $expost ) { |
|---|
| 451 | | if (empty($exclusions)) |
|---|
| 452 | | $exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expost); |
|---|
| 453 | | else |
|---|
| 454 | | $exclusions .= $wpdb->prepare(' AND ID <> %d ', $expost); |
|---|
| 455 | | } |
|---|
| 456 | | } |
|---|
| 457 | | } |
|---|
| 458 | | if (!empty($exclusions)) |
|---|
| 459 | | $exclusions .= ')'; |
|---|
| 460 | | |
|---|
| 461 | | // orderby |
|---|
| 462 | | if ( preg_match( '/.+ +(ASC|DESC)/i', $orderby ) ) |
|---|
| 463 | | $order = ''; // orderby has its own order, so we'll use that |
|---|
| 464 | | |
|---|
| 465 | | $query = "SELECT DISTINCT * FROM $wpdb->posts "; |
|---|
| 466 | | $query .= empty( $category ) ? '' : ", $wpdb->term_relationships, $wpdb->term_taxonomy "; |
|---|
| 467 | | $query .= empty( $meta_key ) ? '' : ", $wpdb->postmeta "; |
|---|
| 468 | | $query .= " WHERE 1=1 "; |
|---|
| 469 | | $query .= empty( $post_type ) ? '' : $wpdb->prepare("AND post_type = %s ", $post_type); |
|---|
| 470 | | $query .= empty( $post_status ) ? '' : $wpdb->prepare("AND post_status = %s ", $post_status); |
|---|
| 471 | | $query .= "$exclusions $inclusions " ; |
|---|
| 472 | | $query .= empty( $category ) ? '' : $wpdb->prepare("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 = %d AND $wpdb->term_taxonomy.taxonomy = 'category')", $category); |
|---|
| 473 | | $query .= empty( $post_parent ) ? '' : $wpdb->prepare("AND $wpdb->posts.post_parent = %d ", $post_parent); |
|---|
| 474 | | // expected_slashed ($meta_key, $meta_value) -- Also, this looks really funky, doesn't seem like it works |
|---|
| 475 | | $query .= empty( $meta_key ) | empty($meta_value) ? '' : $wpdb->prepare(" AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = %s AND $wpdb->postmeta.meta_value = %s )", $meta_key, $meta_value); |
|---|
| 476 | | $query .= empty( $post_mime_type ) ? '' : wp_post_mime_type_where($post_mime_type); |
|---|
| 477 | | $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . ' ' . $order; |
|---|
| 478 | | if ( 0 < $numberposts ) |
|---|
| 479 | | $query .= $wpdb->prepare(" LIMIT %d,%d", $offset, $numberposts); |
|---|
| 480 | | |
|---|
| 481 | | $posts = $wpdb->get_results($query); |
|---|
| 482 | | |
|---|
| 483 | | update_post_caches($posts); |
|---|
| 484 | | |
|---|
| 485 | | return $posts; |
|---|