Use WP_query in get_posts(). Props filosofo. fixes #6772

git-svn-id: http://svn.automattic.com/wordpress/trunk@7880 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-05-03 20:08:32 +00:00
parent fbcbedc4f0
commit d9fd98601e
2 changed files with 33 additions and 70 deletions

View File

@ -402,8 +402,6 @@ function set_post_type( $post_id = 0, $post_type = 'post' ) {
* @return array {@internal Missing Description}}
*/
function get_posts($args = null) {
global $wpdb;
$defaults = array(
'numberposts' => 5, 'offset' => 0,
'category' => 0, 'orderby' => 'post_date',
@ -414,75 +412,20 @@ function get_posts($args = null) {
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
if ( ! empty($r['numberposts']) )
$r['posts_per_page'] = $r['numberposts'];
if ( ! empty($r['category']) )
$r['cat'] = $r['category'];
if ( ! empty($r['include']) ) {
$incposts = preg_split('/[\s,]+/',$r['include']);
$r['posts_per_page'] = count($incposts); // only the number of posts included
$r['post__in'] = $incposts;
} elseif ( ! empty($r['exclude']) )
$r['post__not_in'] = preg_split('/[\s,]+/',$r['exclude']);
$numberposts = (int) $numberposts;
$offset = (int) $offset;
$category = (int) $category;
$post_parent = (int) $post_parent;
$get_posts = new WP_Query;
return $get_posts->query($r);
$inclusions = '';
if ( !empty($include) ) {
$offset = 0; //ignore offset, category, exclude, meta_key, and meta_value, post_parent if using include
$category = 0;
$exclude = '';
$meta_key = '';
$meta_value = '';
$post_parent = 0;
$incposts = preg_split('/[\s,]+/',$include);
$numberposts = count($incposts); // only the number of posts included
if ( count($incposts) ) {
foreach ( $incposts as $incpost ) {
if (empty($inclusions))
$inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpost);
else
$inclusions .= $wpdb->prepare(' OR ID = %d ', $incpost);
}
}
}
if (!empty($inclusions))
$inclusions .= ')';
$exclusions = '';
if ( !empty($exclude) ) {
$exposts = preg_split('/[\s,]+/',$exclude);
if ( count($exposts) ) {
foreach ( $exposts as $expost ) {
if (empty($exclusions))
$exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expost);
else
$exclusions .= $wpdb->prepare(' AND ID <> %d ', $expost);
}
}
}
if (!empty($exclusions))
$exclusions .= ')';
// orderby
if ( preg_match( '/.+ +(ASC|DESC)/i', $orderby ) )
$order = ''; // orderby has its own order, so we'll use that
$query = "SELECT DISTINCT * FROM $wpdb->posts ";
$query .= empty( $category ) ? '' : ", $wpdb->term_relationships, $wpdb->term_taxonomy ";
$query .= empty( $meta_key ) ? '' : ", $wpdb->postmeta ";
$query .= " WHERE 1=1 ";
$query .= empty( $post_type ) ? '' : $wpdb->prepare("AND post_type = %s ", $post_type);
$query .= empty( $post_status ) ? '' : $wpdb->prepare("AND post_status = %s ", $post_status);
$query .= "$exclusions $inclusions " ;
$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);
$query .= empty( $post_parent ) ? '' : $wpdb->prepare("AND $wpdb->posts.post_parent = %d ", $post_parent);
// expected_slashed ($meta_key, $meta_value) -- Also, this looks really funky, doesn't seem like it works
$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);
$query .= empty( $post_mime_type ) ? '' : wp_post_mime_type_where($post_mime_type);
$query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . ' ' . $order;
if ( 0 < $numberposts )
$query .= $wpdb->prepare(" LIMIT %d,%d", $offset, $numberposts);
$posts = $wpdb->get_results($query);
update_post_caches($posts);
return $posts;
}
//

View File

@ -453,6 +453,7 @@ class WP_Query {
'error'
, 'm'
, 'p'
, 'post_parent'
, 'subpost'
, 'subpost_id'
, 'attachment'
@ -477,6 +478,8 @@ class WP_Query {
, 'tb'
, 'paged'
, 'comments_popup'
, 'meta_key'
, 'meta_value'
, 'preview'
);
@ -485,7 +488,7 @@ class WP_Query {
$array[$key] = '';
}
$array_keys = array('category__in', 'category__not_in', 'category__and',
$array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
foreach ( $array_keys as $key ) {
@ -954,6 +957,15 @@ class WP_Query {
// If a post number is specified, load that post
if ( $q['p'] )
$where = " AND {$wpdb->posts}.ID = " . $q['p'];
elseif ( $q['post_parent'] )
$where = $wpdb->prepare("AND $wpdb->posts.post_parent = %d ", $q['post_parent']);
elseif ( $q['post__in'] ) {
$post__in = "'" . implode("', '", $q['post__in']) . "'";
$where = " AND {$wpdb->posts}.ID IN ($post__in)";
} elseif ( $q['post__not_in'] ) {
$post__not_in = "'" . implode("', '", $q['post__not_in']) . "'";
$where = " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
}
if ( $q['page_id'] ) {
if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
@ -1338,6 +1350,14 @@ class WP_Query {
$where .= ')';
}
// postmeta queries
if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) )
$join .= " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
if ( ! empty($q['meta_key']) )
$where .= $wpdb->prepare("AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']);
if ( ! empty($q['meta_value']) )
$where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value = %s ", $q['meta_value']);
// Apply filters on where and join prior to paging so that any
// manipulations to them are reflected in the paging by day queries.
$where = apply_filters('posts_where', $where);