Allow include/exclude args to be arrays in get_posts() and get_pages(). Utilizes wp_parse_id_list(). props scribu, fixes #11076.

git-svn-id: https://develop.svn.wordpress.org/trunk@14133 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Nacin 2010-04-18 04:54:19 +00:00
parent 6e7fed8863
commit 8771fc4af7
1 changed files with 10 additions and 10 deletions

View File

@ -994,8 +994,8 @@ function get_posts($args = null) {
$defaults = array( $defaults = array(
'numberposts' => 5, 'offset' => 0, 'numberposts' => 5, 'offset' => 0,
'category' => 0, 'orderby' => 'post_date', 'category' => 0, 'orderby' => 'post_date',
'order' => 'DESC', 'include' => '', 'order' => 'DESC', 'include' => array(),
'exclude' => '', 'meta_key' => '', 'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'post', 'meta_value' =>'', 'post_type' => 'post',
'suppress_filters' => true 'suppress_filters' => true
); );
@ -1008,11 +1008,11 @@ function get_posts($args = null) {
if ( ! empty($r['category']) ) if ( ! empty($r['category']) )
$r['cat'] = $r['category']; $r['cat'] = $r['category'];
if ( ! empty($r['include']) ) { if ( ! empty($r['include']) ) {
$incposts = preg_split('/[\s,]+/', $r['include']); $incposts = wp_parse_id_list( $r['include'] );
$r['posts_per_page'] = count($incposts); // only the number of posts included $r['posts_per_page'] = count($incposts); // only the number of posts included
$r['post__in'] = $incposts; $r['post__in'] = $incposts;
} elseif ( ! empty($r['exclude']) ) } elseif ( ! empty($r['exclude']) )
$r['post__not_in'] = preg_split('/[\s,]+/',$r['exclude']); $r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
$r['caller_get_posts'] = true; $r['caller_get_posts'] = true;
@ -2934,7 +2934,7 @@ function &get_pages($args = '') {
$defaults = array( $defaults = array(
'child_of' => 0, 'sort_order' => 'ASC', 'child_of' => 0, 'sort_order' => 'ASC',
'sort_column' => 'post_title', 'hierarchical' => 1, 'sort_column' => 'post_title', 'hierarchical' => 1,
'exclude' => '', 'include' => '', 'exclude' => array(), 'include' => array(),
'meta_key' => '', 'meta_value' => '', 'meta_key' => '', 'meta_value' => '',
'authors' => '', 'parent' => -1, 'exclude_tree' => '', 'authors' => '', 'parent' => -1, 'exclude_tree' => '',
'number' => '', 'offset' => 0, 'number' => '', 'offset' => 0,
@ -2975,8 +2975,8 @@ function &get_pages($args = '') {
$meta_key = ''; $meta_key = '';
$meta_value = ''; $meta_value = '';
$hierarchical = false; $hierarchical = false;
$incpages = preg_split('/[\s,]+/',$include); $incpages = wp_parse_id_list( $include );
if ( count($incpages) ) { if ( ! empty( $incpages ) ) {
foreach ( $incpages as $incpage ) { foreach ( $incpages as $incpage ) {
if (empty($inclusions)) if (empty($inclusions))
$inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpage); $inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpage);
@ -2990,8 +2990,8 @@ function &get_pages($args = '') {
$exclusions = ''; $exclusions = '';
if ( !empty($exclude) ) { if ( !empty($exclude) ) {
$expages = preg_split('/[\s,]+/',$exclude); $expages = wp_parse_id_list( $exclude );
if ( count($expages) ) { if ( ! empty( $expages ) ) {
foreach ( $expages as $expage ) { foreach ( $expages as $expage ) {
if (empty($exclusions)) if (empty($exclusions))
$exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expage); $exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expage);
@ -3007,7 +3007,7 @@ function &get_pages($args = '') {
if (!empty($authors)) { if (!empty($authors)) {
$post_authors = preg_split('/[\s,]+/',$authors); $post_authors = preg_split('/[\s,]+/',$authors);
if ( count($post_authors) ) { if ( ! empty( $post_authors ) ) {
foreach ( $post_authors as $post_author ) { foreach ( $post_authors as $post_author ) {
//Do we have an author id or an author login? //Do we have an author id or an author login?
if ( 0 == intval($post_author) ) { if ( 0 == intval($post_author) ) {