Make WordPress Core


Ignore:
Timestamp:
03/21/2006 04:26:50 AM (21 years ago)
Author:
ryan
Message:

Add include param to get_posts(), get_categories(), get_pages(), and get_bookmarks(). Props MichaelH. fixes #2562

File:
1 edited

Legend:

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

    r3654 r3655  
    14471447function get_posts($args) {
    14481448        global $wpdb;
     1449
     1450        if ( is_array($args) )
     1451                $r = &$args;
     1452        else
     1453                parse_str($args, $r);
    14491454        parse_str($args, $r);
    1450         if ( !isset($r['numberposts']) )
    1451                 $r['numberposts'] = 5;
    1452         if ( !isset($r['offset']) )
    1453                 $r['offset'] = 0;
    1454         if ( !isset($r['category']) )
    1455                 $r['category'] = '';
    1456         if ( !isset($r['orderby']) )
    1457                 $r['orderby'] = 'post_date';
    1458         if ( !isset($r['order']) )
    1459                 $r['order'] = 'DESC';
     1455
     1456        $defaults = array('numberposts' => 5, 'offset' => 0, 'category' => '',
     1457                'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '');
     1458        $r = array_merge($defaults, $r);
     1459        extract($r);
     1460
     1461        $inclusions = '';
     1462        if ( !empty($include) ) {
     1463                $offset = 0;    //ignore offset, category, and exclude params if using include
     1464                $category = '';
     1465                $exclude = ''; 
     1466                $incposts = preg_split('/[\s,]+/',$include);
     1467                $numberposts = count($incposts);  // only the number of posts included
     1468                if ( count($incposts) ) {
     1469                        foreach ( $incposts as $incpost ) {
     1470                                if (empty($inclusions))
     1471                                        $inclusions = ' AND ( ID = ' . intval($incpost) . ' ';
     1472                                else
     1473                                        $inclusions .= ' OR ID = ' . intval($incpost) . ' ';
     1474                        }
     1475                }
     1476        }
     1477        if (!empty($inclusions))
     1478                $inclusions .= ')';     
     1479
     1480        $exclusions = '';
     1481        if ( !empty($exclude) ) {
     1482                $exposts = preg_split('/[\s,]+/',$exclude);
     1483                if ( count($exposts) ) {
     1484                        foreach ( $exposts as $expost ) {
     1485                                if (empty($exclusions))
     1486                                        $exclusions = ' AND ( ID <> ' . intval($expost) . ' ';
     1487                                else
     1488                                        $exclusions .= ' AND ID <> ' . intval($expost) . ' ';
     1489                        }
     1490                }
     1491        }
     1492        if (!empty($exclusions))
     1493                $exclusions .= ')';
    14601494
    14611495        $posts = $wpdb->get_results(
    14621496                "SELECT DISTINCT * FROM $wpdb->posts " .
    1463                 ( empty( $r['category'] ) ? "" : ", $wpdb->post2cat " ) .
    1464                 " WHERE (post_type = 'post' AND post_status = 'publish') ".
    1465                 ( empty( $r['category'] ) ? "" : "AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $r['category']. " " ) .
    1466                 " GROUP BY $wpdb->posts.ID ORDER BY " . $r['orderby'] . " " . $r['order'] . " LIMIT " . $r['offset'] . ',' . $r['numberposts'] );
     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 );
    14671501
    14681502        update_post_caches($posts);
Note: See TracChangeset for help on using the changeset viewer.