Make WordPress Core

Ticket #2562: incl_excl_gets.diff

File incl_excl_gets.diff, 9.2 KB (added by MichaelH, 20 years ago)

include/exclude ids for get_posts(), get_pages(), get_categories(), and get_bookmarks()

  • wp-includes/functions.php

     
    14461446
    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';
    14601455
     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 .= ')';
     1494
    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);
    14691503
  • wp-includes/template-functions-bookmarks.php

     
    283283                parse_str($args, $r);
    284284
    285285        $defaults = array('orderby' => 'name', 'order' => 'ASC', 'limit' => -1, 'category' => -1,
    286                 'category_name' => '', 'hide_invisible' => 1, 'show_updated' => 0);
     286                'category_name' => '', 'hide_invisible' => 1, 'show_updated' => 0, 'include' => '', 'exclude' => '');
    287287        $r = array_merge($defaults, $r);
    288288        extract($r);
    289289
     290        $inclusions = '';
     291        if ( !empty($include) ) {
     292        $exclude = '';  //ignore exclude, category, and category_name params if using include
     293        $category = -1;
     294        $category_name = '';
     295                $inclinks = preg_split('/[\s,]+/',$include);
     296                if ( count($inclinks) ) {
     297                        foreach ( $inclinks as $inclink ) {
     298                                if (empty($inclusions))
     299                                        $inclusions = ' AND ( link_id = ' . intval($inclink) . ' ';
     300                                else
     301                                        $inclusions .= ' OR link_id = ' . intval($inclink) . ' ';
     302                        }
     303                }
     304        }
     305        if (!empty($inclusions))
     306                $inclusions .= ')';
     307
    290308        $exclusions = '';
    291309        if ( !empty($exclude) ) {
    292                 $exlinks = preg_split('/[\s,]+/',$r['exclude']);
     310                $exlinks = preg_split('/[\s,]+/',$exclude);
    293311                if ( count($exlinks) ) {
    294312                        foreach ( $exlinks as $exlink ) {
    295                                 $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
     313                                if (empty($exclusions))
     314                                        $exclusions = ' AND ( link_id <> ' . intval($exlink) . ' ';
     315                                else
     316                                        $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
    296317                        }
    297318                }
    298319        }
    299 
     320        if (!empty($exclusions))
     321                $exclusions .= ')';
     322               
    300323        if ( ! empty($category_name) ) {
    301324                if ( $cat_id = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$category_name' LIMIT 1") )
    302325                        $category = $cat_id;
     
    320343                $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
    321344        }
    322345
    323         $orderby = strtolower($r['orderby']);
     346        $orderby = strtolower($orderby);
    324347        $length = '';
    325348        switch ($orderby) {
    326349                case 'length':
     
    341364                $visible = "AND link_visible = 'Y'";
    342365
    343366        $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
     367        $query .= " $exclusions $inclusions";
    344368        $query .= " ORDER BY $orderby $order";
    345369        if ($limit != -1)
    346370                $query .= " LIMIT $limit";
  • wp-includes/template-functions-category.php

     
    376376                parse_str($args, $r);
    377377
    378378        $defaults = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC',
    379                 'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1);
     379                'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1, $exclude => '', $include => '');
    380380        $r = array_merge($defaults, $r);
    381         $r['orderby'] = "cat_" . $r['orderby'];
     381        $r['orderby'] = "cat_" . $r['orderby'];  // restricts order by to cat_ID and cat_name fields
    382382        extract($r);
    383383
    384         $exclusions = '';
    385         $having = '';
    386384        $where = 'cat_ID > 0';
     385        $inclusions = '';
     386        if ( !empty($include) ) {
     387                $child_of = 0; //ignore child_of and exclude params if using include
     388                $exclude = ''; 
     389                $incategories = preg_split('/[\s,]+/',$include);
     390                if ( count($incategories) ) {
     391                        foreach ( $incategories as $incat ) {
     392                                if (empty($inclusions))
     393                                        $inclusions = ' AND ( cat_ID = ' . intval($incat) . ' ';
     394                                else
     395                                        $inclusions .= ' OR cat_ID = ' . intval($incat) . ' ';
     396                        }
     397                }
     398        }
     399        if (!empty($inclusions))
     400                $inclusions .= ')';     
     401        $where .= $inclusions;
    387402
    388403        $exclusions = '';
    389404        if ( !empty($exclude) ) {
    390405                $excategories = preg_split('/[\s,]+/',$exclude);
    391406                if ( count($excategories) ) {
    392407                        foreach ( $excategories as $excat ) {
    393                                 $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
    394                                 // TODO: Exclude children of excluded cats?
     408                                if (empty($exclusions))
     409                                        $exclusions = ' AND ( cat_ID <> ' . intval($excat) . ' ';
     410                                else
     411                                        $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
     412                                // TODO: Exclude children of excluded cats?   Note: children are getting excluded
    395413                        }
    396414                }
    397415        }
     416        if (!empty($exclusions))
     417                $exclusions .= ')';
    398418        $exclusions = apply_filters('list_cats_exclusions', $exclusions );
    399419        $where .= $exclusions;
    400420
     421        $having = '';
    401422        if ( $hide_empty ) {
    402423                if ( 'link' == $type )
    403424                        $having = 'HAVING link_count > 0';
  • 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);
     309                'hierarchical' => 1, $exclude => '', $include => '');
    310310        $r = array_merge($defaults, $r);
     311        extract($r);
    311312
     313        $inclusions = '';
     314        if ( !empty($include) ) {
     315                $child_of = 0; //ignore child_of and exclude params if using include
     316                $exclude = ''; 
     317                $incpages = preg_split('/[\s,]+/',$include);
     318                if ( count($incpages) ) {
     319                        foreach ( $incpages as $incpage ) {
     320                                if (empty($inclusions))
     321                                        $inclusions = ' AND ( ID = ' . intval($incpage) . ' ';
     322                                else
     323                                        $inclusions .= ' OR ID = ' . intval($incpage) . ' ';
     324                        }
     325                }
     326        }
     327        if (!empty($inclusions))
     328                $inclusions .= ')';     
     329
    312330        $exclusions = '';
    313         if ( !empty($r['exclude']) ) {
    314                 $expages = preg_split('/[\s,]+/',$r['exclude']);
     331        if ( !empty($exclude) ) {
     332                $expages = preg_split('/[\s,]+/',$exclude);
    315333                if ( count($expages) ) {
    316334                        foreach ( $expages as $expage ) {
    317                                 $exclusions .= ' AND ID <> ' . intval($expage) . ' ';
     335                                if (empty($exclusions))
     336                                        $exclusions = ' AND ( ID <> ' . intval($expage) . ' ';
     337                                else
     338                                        $exclusions .= ' AND ID <> ' . intval($expage) . ' ';
    318339                        }
    319340                }
    320341        }
     342        if (!empty($exclusions))
     343                $exclusions .= ')';
    321344
    322345        $pages = $wpdb->get_results("SELECT * " .
    323346                "FROM $wpdb->posts " .
    324347                "WHERE post_type = 'page' AND post_status = 'publish' " .
    325                 "$exclusions " .
    326                 "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']);
     348                "$exclusions $inclusions" .
     349                "ORDER BY " . $sort_column . " " . $sort_order);
    327350
    328351        if ( empty($pages) )
    329352                return array();
     
    331354        // Update cache.
    332355        update_page_cache($pages);
    333356
    334         if ( $r['child_of'] || $r['hierarchical'] )
    335                 $pages = & get_page_children($r['child_of'], $pages);
     357        if ( $child_of || $hierarchical )
     358                $pages = & get_page_children($child_of, $pages);
    336359
    337360        return $pages;
    338361}