Changeset 3656

Show
Ignore:
Timestamp:
03/21/06 22:46:38 (2 years ago)
Author:
ryan
Message:

Add meta_key and meta_value options to get_posts() and get_pages(). Props MichaelH. fixes #2563

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-includes/functions.php

    r3655 r3656  
    14521452    else 
    14531453        parse_str($args, $r); 
    1454     parse_str($args, $r); 
    14551454 
    14561455    $defaults = array('numberposts' => 5, 'offset' => 0, 'category' => '', 
    1457         'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => ''); 
     1456        'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' =>''); 
    14581457    $r = array_merge($defaults, $r); 
    14591458    extract($r); 
     
    14611460    $inclusions = ''; 
    14621461    if ( !empty($include) ) { 
    1463         $offset = 0;    //ignore offset, category, and exclude params if using include 
     1462        $offset = 0;    //ignore offset, category, exclude, meta_key, and meta_value params if using include 
    14641463        $category = '';  
    14651464        $exclude = '';   
     1465        $meta_key = ''; 
     1466        $meta_value = ''; 
    14661467        $incposts = preg_split('/[\s,]+/',$include); 
    14671468        $numberposts = count($incposts);  // only the number of posts included 
     
    14931494        $exclusions .= ')'; 
    14941495 
    1495     $posts = $wpdb->get_results( 
    1496         "SELECT DISTINCT * FROM $wpdb->posts " . 
    1497         ( empty( $category ) ? "" : ", $wpdb->post2cat " ) . 
    1498         " WHERE (post_type = 'post' AND post_status = 'publish') $exclusions $inclusions " . 
    1499         ( empty( $category ) ? "" : "AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. " " ) . 
    1500         " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . " " . $order . " LIMIT " . $offset . ',' . $numberposts ); 
     1496    $query ="SELECT DISTINCT * FROM $wpdb->posts " ; 
     1497    $query .= ( empty( $category ) ? "" : ", $wpdb->post2cat " ) ;  
     1498    $query .= ( empty( $meta_key ) ? "" : ", $wpdb->postmeta " ) ;  
     1499    $query .= " WHERE (post_type = 'post' AND post_status = 'publish') $exclusions $inclusions " ; 
     1500    $query .= ( empty( $category ) ? "" : "AND ($wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. ") " ) ; 
     1501    $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' )" ) ; 
     1502    $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . " " . $order . " LIMIT " . $offset . ',' . $numberposts ; 
     1503 
     1504    $posts = $wpdb->get_results($query); 
    15011505 
    15021506    update_post_caches($posts); 
  • trunk/wp-includes/template-functions-post.php

    r3655 r3656  
    307307 
    308308    $defaults = array('child_of' => 0, 'sort_order' => 'ASC', 'sort_column' => 'post_title', 
    309         'hierarchical' => 1, $exclude => '', $include => ''); 
     309               'hierarchical' => 1, 'exclude' => '', 'include' => '', 'meta_key' => '', 'meta_value' => ''); 
    310310    $r = array_merge($defaults, $r); 
    311311    extract($r); 
     
    313313    $inclusions = ''; 
    314314    if ( !empty($include) ) { 
    315         $child_of = 0; //ignore child_of and exclude params if using include  
     315        $child_of = 0; //ignore child_of, exclude, meta_key, and meta_value params if using include  
    316316        $exclude = '';   
     317        $meta_key = ''; 
     318        $meta_value = ''; 
    317319        $incpages = preg_split('/[\s,]+/',$include); 
    318320        if ( count($incpages) ) { 
     
    343345        $exclusions .= ')'; 
    344346 
    345     $pages = $wpdb->get_results("SELECT * " . 
    346         "FROM $wpdb->posts " . 
    347         "WHERE post_type = 'page' AND post_status = 'publish' " . 
    348         "$exclusions $inclusions" . 
    349         "ORDER BY " . $sort_column . " " . $sort_order); 
     347    $query = "SELECT * FROM $wpdb->posts " ; 
     348    $query .= ( empty( $meta_key ) ? "" : ", $wpdb->postmeta " ) ;  
     349    $query .= " WHERE (post_type = 'page' AND post_status = 'publish') $exclusions $inclusions " ; 
     350    $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' )" ) ; 
     351    $query .= " ORDER BY " . $sort_column . " " . $sort_order ; 
     352 
     353    $pages = $wpdb->get_results($query); 
    350354 
    351355    if ( empty($pages) )