Make WordPress Core

Ticket #2563: postmeta_val_test_get_posts_and_get_pages.diff

File postmeta_val_test_get_posts_and_get_pages.diff, 4.2 KB (added by MichaelH, 21 years ago)

In get_posts() and get_pages() test meta_key and meta_value

  • wp-includes/functions.php

     
    14541454        parse_str($args, $r);
    14551455
    14561456        $defaults = array('numberposts' => 5, 'offset' => 0, 'category' => '',
    1457                 'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '');
     1457                'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' =>'');
    14581458        $r = array_merge($defaults, $r);
    14591459        extract($r);
    14601460
    14611461        $inclusions = '';
    14621462        if ( !empty($include) ) {
    1463                 $offset = 0;    //ignore offset, category, and exclude params if using include
     1463                $offset = 0;    //ignore offset, category, exclude, meta_key, and meta_value params if using include
    14641464                $category = '';
    14651465                $exclude = ''; 
     1466                $meta_key = '';
     1467                $meta_value = '';
    14661468                $incposts = preg_split('/[\s,]+/',$include);
    14671469                $numberposts = count($incposts);  // only the number of posts included
    14681470                if ( count($incposts) ) {
    … …  
    14921494        if (!empty($exclusions))
    14931495                $exclusions .= ')';
    14941496
    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 );
     1497        $query ="SELECT DISTINCT * FROM $wpdb->posts " ;
     1498        $query .= ( empty( $category ) ? "" : ", $wpdb->post2cat " ) ;
     1499        $query .= ( empty( $meta_key ) ? "" : ", $wpdb->postmeta " ) ;
     1500        $query .= " WHERE (post_type = 'post' AND post_status = 'publish') $exclusions $inclusions " ;
     1501        $query .= ( empty( $category ) ? "" : "AND ($wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. ") " ) ;
     1502        $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' )" ) ;
     1503        $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . " " . $order . " LIMIT " . $offset . ',' . $numberposts ;
    15011504
     1505        $posts = $wpdb->get_results($query);
     1506
    15021507        update_post_caches($posts);
    15031508
    15041509        return $posts;
  • wp-includes/template-functions-post.php

     
    306306                parse_str($args, $r);
    307307
    308308        $defaults = array('child_of' => 0, 'sort_order' => 'ASC', 'sort_column' => 'post_title',
    309                 'hierarchical' => 1, $exclude => '', $include => '');
    310         $r = array_merge($defaults, $r);
     309                                'hierarchical' => 1, 'exclude' => '', 'include' => '', 'meta_key' => '', 'meta_value' =>'');
     310 array_merge($defaults, $r);
    311311        extract($r);
    312312
    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) ) {
    319321                        foreach ( $incpages as $incpage ) {
    … …  
    342344        if (!empty($exclusions))
    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 ;
    350352
     353        $pages = $wpdb->get_results($query);
     354
    351355        if ( empty($pages) )
    352356                return array();
    353357