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
This commit is contained in:
ryan 2006-03-21 04:26:50 +00:00
parent b5b8986bca
commit 4ce7df114d
4 changed files with 141 additions and 39 deletions

View File

@ -1446,24 +1446,58 @@ function get_page_uri($page_id) {
function get_posts($args) { function get_posts($args) {
global $wpdb; global $wpdb;
if ( is_array($args) )
$r = &$args;
else
parse_str($args, $r);
parse_str($args, $r); parse_str($args, $r);
if ( !isset($r['numberposts']) )
$r['numberposts'] = 5; $defaults = array('numberposts' => 5, 'offset' => 0, 'category' => '',
if ( !isset($r['offset']) ) 'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '');
$r['offset'] = 0; $r = array_merge($defaults, $r);
if ( !isset($r['category']) ) extract($r);
$r['category'] = '';
if ( !isset($r['orderby']) ) $inclusions = '';
$r['orderby'] = 'post_date'; if ( !empty($include) ) {
if ( !isset($r['order']) ) $offset = 0; //ignore offset, category, and exclude params if using include
$r['order'] = 'DESC'; $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( $posts = $wpdb->get_results(
"SELECT DISTINCT * FROM $wpdb->posts " . "SELECT DISTINCT * FROM $wpdb->posts " .
( empty( $r['category'] ) ? "" : ", $wpdb->post2cat " ) . ( empty( $category ) ? "" : ", $wpdb->post2cat " ) .
" WHERE (post_type = 'post' AND post_status = 'publish') ". " WHERE (post_type = 'post' AND post_status = 'publish') $exclusions $inclusions " .
( empty( $r['category'] ) ? "" : "AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $r['category']. " " ) . ( empty( $category ) ? "" : "AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. " " ) .
" GROUP BY $wpdb->posts.ID ORDER BY " . $r['orderby'] . " " . $r['order'] . " LIMIT " . $r['offset'] . ',' . $r['numberposts'] ); " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . " " . $order . " LIMIT " . $offset . ',' . $numberposts );
update_post_caches($posts); update_post_caches($posts);

View File

@ -283,20 +283,43 @@ function get_bookmarks($args = '') {
parse_str($args, $r); parse_str($args, $r);
$defaults = array('orderby' => 'name', 'order' => 'ASC', 'limit' => -1, 'category' => -1, $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); $r = array_merge($defaults, $r);
extract($r); extract($r);
$exclusions = ''; $inclusions = '';
if ( !empty($exclude) ) { if ( !empty($include) ) {
$exlinks = preg_split('/[\s,]+/',$r['exclude']); $exclude = ''; //ignore exclude, category, and category_name params if using include
if ( count($exlinks) ) { $category = -1;
foreach ( $exlinks as $exlink ) { $category_name = '';
$exclusions .= ' AND link_id <> ' . intval($exlink) . ' '; $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 ( ! empty($category_name) ) {
if ( $cat_id = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$category_name' LIMIT 1") ) if ( $cat_id = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$category_name' LIMIT 1") )
$category = $cat_id; $category = $cat_id;
@ -320,7 +343,7 @@ function get_bookmarks($args = '') {
$get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f "; $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
} }
$orderby = strtolower($r['orderby']); $orderby = strtolower($orderby);
$length = ''; $length = '';
switch ($orderby) { switch ($orderby) {
case 'length': case 'length':
@ -341,6 +364,7 @@ function get_bookmarks($args = '') {
$visible = "AND link_visible = 'Y'"; $visible = "AND link_visible = 'Y'";
$query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query"; $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"; $query .= " ORDER BY $orderby $order";
if ($limit != -1) if ($limit != -1)
$query .= " LIMIT $limit"; $query .= " LIMIT $limit";

View File

@ -376,28 +376,49 @@ function &get_categories($args = '') {
parse_str($args, $r); parse_str($args, $r);
$defaults = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC', $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 = 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); extract($r);
$exclusions = '';
$having = '';
$where = 'cat_ID > 0'; $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 = ''; $exclusions = '';
if ( !empty($exclude) ) { if ( !empty($exclude) ) {
$excategories = preg_split('/[\s,]+/',$exclude); $excategories = preg_split('/[\s,]+/',$exclude);
if ( count($excategories) ) { if ( count($excategories) ) {
foreach ( $excategories as $excat ) { foreach ( $excategories as $excat ) {
$exclusions .= ' AND cat_ID <> ' . intval($excat) . ' '; if (empty($exclusions))
// TODO: Exclude children of excluded cats? $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 ); $exclusions = apply_filters('list_cats_exclusions', $exclusions );
$where .= $exclusions; $where .= $exclusions;
$having = '';
if ( $hide_empty ) { if ( $hide_empty ) {
if ( 'link' == $type ) if ( 'link' == $type )
$having = 'HAVING link_count > 0'; $having = 'HAVING link_count > 0';

View File

@ -306,24 +306,47 @@ function &get_pages($args = '') {
parse_str($args, $r); parse_str($args, $r);
$defaults = array('child_of' => 0, 'sort_order' => 'ASC', 'sort_column' => 'post_title', $defaults = array('child_of' => 0, 'sort_order' => 'ASC', 'sort_column' => 'post_title',
'hierarchical' => 1); 'hierarchical' => 1, $exclude => '', $include => '');
$r = array_merge($defaults, $r); $r = array_merge($defaults, $r);
extract($r);
$exclusions = ''; $inclusions = '';
if ( !empty($r['exclude']) ) { if ( !empty($include) ) {
$expages = preg_split('/[\s,]+/',$r['exclude']); $child_of = 0; //ignore child_of and exclude params if using include
if ( count($expages) ) { $exclude = '';
foreach ( $expages as $expage ) { $incpages = preg_split('/[\s,]+/',$include);
$exclusions .= ' AND ID <> ' . intval($expage) . ' '; 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 * " . $pages = $wpdb->get_results("SELECT * " .
"FROM $wpdb->posts " . "FROM $wpdb->posts " .
"WHERE post_type = 'page' AND post_status = 'publish' " . "WHERE post_type = 'page' AND post_status = 'publish' " .
"$exclusions " . "$exclusions $inclusions" .
"ORDER BY " . $r['sort_column'] . " " . $r['sort_order']); "ORDER BY " . $sort_column . " " . $sort_order);
if ( empty($pages) ) if ( empty($pages) )
return array(); return array();
@ -331,8 +354,8 @@ function &get_pages($args = '') {
// Update cache. // Update cache.
update_page_cache($pages); update_page_cache($pages);
if ( $r['child_of'] || $r['hierarchical'] ) if ( $child_of || $hierarchical )
$pages = & get_page_children($r['child_of'], $pages); $pages = & get_page_children($child_of, $pages);
return $pages; return $pages;
} }