From 4ce7df114d94e5439646e4c408e2b19cde067184 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 21 Mar 2006 04:26:50 +0000 Subject: [PATCH] Add include param to get_posts(), get_categories(), get_pages(), and get_bookmarks(). Props MichaelH. fixes #2562 git-svn-id: http://svn.automattic.com/wordpress/trunk@3655 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 62 +++++++++++++++----- wp-includes/template-functions-bookmarks.php | 40 ++++++++++--- wp-includes/template-functions-category.php | 33 +++++++++-- wp-includes/template-functions-post.php | 45 ++++++++++---- 4 files changed, 141 insertions(+), 39 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 513c242936..322ba46749 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -1446,24 +1446,58 @@ function get_page_uri($page_id) { function get_posts($args) { global $wpdb; + + if ( is_array($args) ) + $r = &$args; + else + parse_str($args, $r); parse_str($args, $r); - if ( !isset($r['numberposts']) ) - $r['numberposts'] = 5; - if ( !isset($r['offset']) ) - $r['offset'] = 0; - if ( !isset($r['category']) ) - $r['category'] = ''; - if ( !isset($r['orderby']) ) - $r['orderby'] = 'post_date'; - if ( !isset($r['order']) ) - $r['order'] = 'DESC'; + + $defaults = array('numberposts' => 5, 'offset' => 0, 'category' => '', + 'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => ''); + $r = array_merge($defaults, $r); + extract($r); + + $inclusions = ''; + if ( !empty($include) ) { + $offset = 0; //ignore offset, category, and exclude params if using include + $category = ''; + $exclude = ''; + $incposts = preg_split('/[\s,]+/',$include); + $numberposts = count($incposts); // only the number of posts included + if ( count($incposts) ) { + foreach ( $incposts as $incpost ) { + if (empty($inclusions)) + $inclusions = ' AND ( ID = ' . intval($incpost) . ' '; + else + $inclusions .= ' OR ID = ' . intval($incpost) . ' '; + } + } + } + if (!empty($inclusions)) + $inclusions .= ')'; + + $exclusions = ''; + if ( !empty($exclude) ) { + $exposts = preg_split('/[\s,]+/',$exclude); + if ( count($exposts) ) { + foreach ( $exposts as $expost ) { + if (empty($exclusions)) + $exclusions = ' AND ( ID <> ' . intval($expost) . ' '; + else + $exclusions .= ' AND ID <> ' . intval($expost) . ' '; + } + } + } + if (!empty($exclusions)) + $exclusions .= ')'; $posts = $wpdb->get_results( "SELECT DISTINCT * FROM $wpdb->posts " . - ( empty( $r['category'] ) ? "" : ", $wpdb->post2cat " ) . - " WHERE (post_type = 'post' AND post_status = 'publish') ". - ( empty( $r['category'] ) ? "" : "AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $r['category']. " " ) . - " GROUP BY $wpdb->posts.ID ORDER BY " . $r['orderby'] . " " . $r['order'] . " LIMIT " . $r['offset'] . ',' . $r['numberposts'] ); + ( empty( $category ) ? "" : ", $wpdb->post2cat " ) . + " WHERE (post_type = 'post' AND post_status = 'publish') $exclusions $inclusions " . + ( empty( $category ) ? "" : "AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. " " ) . + " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . " " . $order . " LIMIT " . $offset . ',' . $numberposts ); update_post_caches($posts); diff --git a/wp-includes/template-functions-bookmarks.php b/wp-includes/template-functions-bookmarks.php index 7a51da48a1..d85c196081 100644 --- a/wp-includes/template-functions-bookmarks.php +++ b/wp-includes/template-functions-bookmarks.php @@ -283,20 +283,43 @@ function get_bookmarks($args = '') { parse_str($args, $r); $defaults = array('orderby' => 'name', 'order' => 'ASC', 'limit' => -1, 'category' => -1, - 'category_name' => '', 'hide_invisible' => 1, 'show_updated' => 0); + 'category_name' => '', 'hide_invisible' => 1, 'show_updated' => 0, 'include' => '', 'exclude' => ''); $r = array_merge($defaults, $r); extract($r); - $exclusions = ''; - if ( !empty($exclude) ) { - $exlinks = preg_split('/[\s,]+/',$r['exclude']); - if ( count($exlinks) ) { - foreach ( $exlinks as $exlink ) { - $exclusions .= ' AND link_id <> ' . intval($exlink) . ' '; + $inclusions = ''; + if ( !empty($include) ) { + $exclude = ''; //ignore exclude, category, and category_name params if using include + $category = -1; + $category_name = ''; + $inclinks = preg_split('/[\s,]+/',$include); + if ( count($inclinks) ) { + foreach ( $inclinks as $inclink ) { + if (empty($inclusions)) + $inclusions = ' AND ( link_id = ' . intval($inclink) . ' '; + else + $inclusions .= ' OR link_id = ' . intval($inclink) . ' '; } } } + if (!empty($inclusions)) + $inclusions .= ')'; + $exclusions = ''; + if ( !empty($exclude) ) { + $exlinks = preg_split('/[\s,]+/',$exclude); + if ( count($exlinks) ) { + foreach ( $exlinks as $exlink ) { + if (empty($exclusions)) + $exclusions = ' AND ( link_id <> ' . intval($exlink) . ' '; + else + $exclusions .= ' AND link_id <> ' . intval($exlink) . ' '; + } + } + } + if (!empty($exclusions)) + $exclusions .= ')'; + if ( ! empty($category_name) ) { if ( $cat_id = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$category_name' LIMIT 1") ) $category = $cat_id; @@ -320,7 +343,7 @@ function get_bookmarks($args = '') { $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f "; } - $orderby = strtolower($r['orderby']); + $orderby = strtolower($orderby); $length = ''; switch ($orderby) { case 'length': @@ -341,6 +364,7 @@ function get_bookmarks($args = '') { $visible = "AND link_visible = 'Y'"; $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query"; + $query .= " $exclusions $inclusions"; $query .= " ORDER BY $orderby $order"; if ($limit != -1) $query .= " LIMIT $limit"; diff --git a/wp-includes/template-functions-category.php b/wp-includes/template-functions-category.php index 1ee8f63406..ae9a344dae 100644 --- a/wp-includes/template-functions-category.php +++ b/wp-includes/template-functions-category.php @@ -376,28 +376,49 @@ function &get_categories($args = '') { parse_str($args, $r); $defaults = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC', - 'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1); + 'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1, $exclude => '', $include => ''); $r = array_merge($defaults, $r); - $r['orderby'] = "cat_" . $r['orderby']; + $r['orderby'] = "cat_" . $r['orderby']; // restricts order by to cat_ID and cat_name fields extract($r); - $exclusions = ''; - $having = ''; $where = 'cat_ID > 0'; + $inclusions = ''; + if ( !empty($include) ) { + $child_of = 0; //ignore child_of and exclude params if using include + $exclude = ''; + $incategories = preg_split('/[\s,]+/',$include); + if ( count($incategories) ) { + foreach ( $incategories as $incat ) { + if (empty($inclusions)) + $inclusions = ' AND ( cat_ID = ' . intval($incat) . ' '; + else + $inclusions .= ' OR cat_ID = ' . intval($incat) . ' '; + } + } + } + if (!empty($inclusions)) + $inclusions .= ')'; + $where .= $inclusions; $exclusions = ''; if ( !empty($exclude) ) { $excategories = preg_split('/[\s,]+/',$exclude); if ( count($excategories) ) { foreach ( $excategories as $excat ) { - $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' '; - // TODO: Exclude children of excluded cats? + if (empty($exclusions)) + $exclusions = ' AND ( cat_ID <> ' . intval($excat) . ' '; + else + $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' '; + // TODO: Exclude children of excluded cats? Note: children are getting excluded } } } + if (!empty($exclusions)) + $exclusions .= ')'; $exclusions = apply_filters('list_cats_exclusions', $exclusions ); $where .= $exclusions; + $having = ''; if ( $hide_empty ) { if ( 'link' == $type ) $having = 'HAVING link_count > 0'; diff --git a/wp-includes/template-functions-post.php b/wp-includes/template-functions-post.php index b3dc177fbf..794eded6ac 100644 --- a/wp-includes/template-functions-post.php +++ b/wp-includes/template-functions-post.php @@ -306,24 +306,47 @@ function &get_pages($args = '') { parse_str($args, $r); $defaults = array('child_of' => 0, 'sort_order' => 'ASC', 'sort_column' => 'post_title', - 'hierarchical' => 1); + 'hierarchical' => 1, $exclude => '', $include => ''); $r = array_merge($defaults, $r); + extract($r); - $exclusions = ''; - if ( !empty($r['exclude']) ) { - $expages = preg_split('/[\s,]+/',$r['exclude']); - if ( count($expages) ) { - foreach ( $expages as $expage ) { - $exclusions .= ' AND ID <> ' . intval($expage) . ' '; + $inclusions = ''; + if ( !empty($include) ) { + $child_of = 0; //ignore child_of and exclude params if using include + $exclude = ''; + $incpages = preg_split('/[\s,]+/',$include); + if ( count($incpages) ) { + foreach ( $incpages as $incpage ) { + if (empty($inclusions)) + $inclusions = ' AND ( ID = ' . intval($incpage) . ' '; + else + $inclusions .= ' OR ID = ' . intval($incpage) . ' '; } } } + if (!empty($inclusions)) + $inclusions .= ')'; + + $exclusions = ''; + if ( !empty($exclude) ) { + $expages = preg_split('/[\s,]+/',$exclude); + if ( count($expages) ) { + foreach ( $expages as $expage ) { + if (empty($exclusions)) + $exclusions = ' AND ( ID <> ' . intval($expage) . ' '; + else + $exclusions .= ' AND ID <> ' . intval($expage) . ' '; + } + } + } + if (!empty($exclusions)) + $exclusions .= ')'; $pages = $wpdb->get_results("SELECT * " . "FROM $wpdb->posts " . "WHERE post_type = 'page' AND post_status = 'publish' " . - "$exclusions " . - "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']); + "$exclusions $inclusions" . + "ORDER BY " . $sort_column . " " . $sort_order); if ( empty($pages) ) return array(); @@ -331,8 +354,8 @@ function &get_pages($args = '') { // Update cache. update_page_cache($pages); - if ( $r['child_of'] || $r['hierarchical'] ) - $pages = & get_page_children($r['child_of'], $pages); + if ( $child_of || $hierarchical ) + $pages = & get_page_children($child_of, $pages); return $pages; }