Make WordPress Core

Opened 6 years ago

Closed 5 years ago

Last modified 4 years ago

#11076 closed enhancement (fixed)

Allow 'include' & 'exclude' args to be arrays

Reported by: scribu Owned by: scribu
Milestone: 3.0 Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch commit
Focuses: Cc:


Most of the time, the term ids you want to include are retrieved as an array, using $wpdb.

To avoid having to join(',', $term_ids), only to have them preg_split() afterwards, I think this would be really useful.

Attachments (5)

get_terms.diff (862 bytes) - added by scribu 6 years ago.
get_terms.2.diff (864 bytes) - added by scribu 6 years ago.
Fix copy-paste error
get_terms.3.diff (4.6 KB) - added by scribu 6 years ago.
Use helper function
get_terms.4.diff (5.0 KB) - added by scribu 6 years ago.
Rename helper function to wp_parse_id_list()
post.php.diff (2.8 KB) - added by scribu 5 years ago.
Use wp_parse_id_list() in get_posts() and get_pages()

Download all attachments as: .zip

Change History (21)

@scribu6 years ago

comment:1 @ryan6 years ago

Looks like a cut-and-paste error. Second $include should be $exclude.

comment:2 @ryan6 years ago

  • Milestone changed from 2.9 to 3.0

@scribu6 years ago

Fix copy-paste error

comment:3 @scribu6 years ago

  • Keywords commit added; tested removed

comment:4 @westi6 years ago

Looks like we should also do this for $exclude_tree and we should probably set the default values at the top of the function to array() instead of '' so we can avoid the preg_split.

Wondering if we should refactor some of this code into a helper function as we have a lot of copy/paste going on here.

comment:5 @scribu6 years ago

get_terms.3.diff creates a helper function called _parse_get_terms_arg() and applies it to exclude, exclude_tree and include.

@scribu6 years ago

Use helper function

@scribu6 years ago

Rename helper function to wp_parse_id_list()

comment:6 @scribu6 years ago

get_terms.4.diff moves the helper function wp_parse_id_list() to wp-includes/functions.php so that it can be used in other contexts.

comment:7 @westi6 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [12658]) Add support to get_terms() to allow 'include' & 'exclude' args to be arrays(). Fixes #11076 props scribu.

comment:8 @sirzooro6 years ago

I think it will be better to change generated SQL from:

AND (t.term_id = 1 OR t.term_id = 2 OR ...)
AND (t.term_id <> 1 AND t.term_id <> 2 AND ...)


AND t.term_id IN (1, 2, ...)
AND t.term_id NOT IN (1, 2, ...)

comment:9 @scribu6 years ago

+1 on that. Please open a new ticket.

comment:10 @scribu5 years ago

  • Keywords needs-patch added; has-patch commit removed
  • Resolution fixed deleted
  • Status changed from closed to reopened
  • Summary changed from In get_terms(), allow 'include' & 'exclude' args to be arrays to Allow 'include' & 'exclude' args to be arrays

Should also do this for get_pages() and get_posts()

comment:11 @scribu5 years ago

  • Component changed from Taxonomy to General

@scribu5 years ago

Use wp_parse_id_list() in get_posts() and get_pages()

comment:12 @scribu5 years ago

  • Keywords has-patch commit added; needs-patch removed


  • use wp_parse_id_list() in get_posts() and get_pages()
  • replace if ( count(...) ) with if ( !empty(...) )

comment:13 @scribu5 years ago

  • Owner changed from filosofo to scribu
  • Status changed from reopened to accepted

comment:14 @nacin5 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

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

comment:15 @miqrogroove4 years ago

I'm trying to figure out if something blew up here. In version 2.9 this was working fine:

        $ids = implode(',', $ids);
        $taxonomy = 'post_tag';
        $args = array('include' => $ids);
        $terms = get_terms($taxonomy, $args);

In version 3.0, get_terms is returning an empty array :( I'm working on a plugin upgrade that needs to be backward compatible, so I'm hoping this change doesn't mean CSVs are invalid now.

comment:16 @miqrogroove4 years ago

Nevermind, it works fine. The tags I was testing were attached to drafts so they were coming up empty. Inputs handled as expected:

array(1) {
Note: See TracTickets for help on using tickets.