WordPress.org

Make WordPress Core

Changeset 7880


Ignore:
Timestamp:
05/03/08 20:08:32 (7 years ago)
Author:
ryan
Message:

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

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r7876 r7880  
    403403 */ 
    404404function get_posts($args = null) { 
    405     global $wpdb; 
    406  
    407405    $defaults = array( 
    408406        'numberposts' => 5, 'offset' => 0, 
     
    415413 
    416414    $r = wp_parse_args( $args, $defaults ); 
    417     extract( $r, EXTR_SKIP ); 
    418  
    419     $numberposts = (int) $numberposts; 
    420     $offset = (int) $offset; 
    421     $category = (int) $category; 
    422     $post_parent = (int) $post_parent; 
    423  
    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; 
     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']); 
     425 
     426    $get_posts = new WP_Query; 
     427    return $get_posts->query($r); 
     428 
    486429} 
    487430 
  • trunk/wp-includes/query.php

    r7685 r7880  
    454454            , 'm' 
    455455            , 'p' 
     456            , 'post_parent' 
    456457            , 'subpost' 
    457458            , 'subpost_id' 
     
    478479            , 'paged' 
    479480            , 'comments_popup' 
     481            , 'meta_key' 
     482            , 'meta_value' 
    480483            , 'preview' 
    481484        ); 
     
    486489        } 
    487490 
    488         $array_keys = array('category__in', 'category__not_in', 'category__and', 
     491        $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in', 
    489492            'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and'); 
    490493 
     
    955958        if ( $q['p'] ) 
    956959            $where = " AND {$wpdb->posts}.ID = " . $q['p']; 
     960        elseif ( $q['post_parent'] )  
     961            $where = $wpdb->prepare("AND $wpdb->posts.post_parent = %d ", $q['post_parent']); 
     962        elseif ( $q['post__in'] ) { 
     963            $post__in = "'" . implode("', '", $q['post__in']) . "'"; 
     964            $where = " AND {$wpdb->posts}.ID IN ($post__in)"; 
     965        } elseif ( $q['post__not_in'] ) { 
     966            $post__not_in = "'" . implode("', '", $q['post__not_in']) . "'"; 
     967            $where = " AND {$wpdb->posts}.ID NOT IN ($post__not_in)"; 
     968        } 
    957969 
    958970        if ( $q['page_id'] ) { 
     
    13381350            $where .= ')'; 
    13391351        } 
     1352 
     1353        // postmeta queries 
     1354        if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) ) 
     1355            $join .= " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) "; 
     1356        if ( ! empty($q['meta_key']) )  
     1357            $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']); 
     1358        if ( ! empty($q['meta_value']) ) 
     1359            $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value = %s ", $q['meta_value']); 
    13401360 
    13411361        // Apply filters on where and join prior to paging so that any 
Note: See TracChangeset for help on using the changeset viewer.