Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#43747 closed defect (bug) (invalid)

$query set post__not_in not accepting inputs from variables correctly

Reported by: venutius's profile Venutius Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Query Keywords:
Focuses: Cc:

Description

If I enter:

$query->set('post__not_in', array(316,319,321));

posts 316, 319 and 321 are excluded from the loop.

However, if I do:

$exclude_string = '316,319,321';
$query->set('post__not_in', array($exclude_string));

Only post 316 is excluded. Can't see where I'd be doing something wrong here.

Change History (1)

#1 @swissspidy
7 years ago

  • Component changed from General to Query
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
  • Summary changed from $query set post__not_in not accepting inputs from varaibles correctly to $query set post__not_in not accepting inputs from variables correctly

Hi there

The documentation for post__not_in is pretty clear:

An array of post IDs not to retrieve. Note: a string of comma-separated IDs will NOT work.

array(316,319,321) is an array of integers, which are post IDs.

In your example, array($exclude_string) is the same as writing array( '316,319,321' ). This is an array with one item, a string containing a comma-separated list of post IDs.

To make your code work, just use $query->set( 'post__not_in', array( implode( ',', $exclude_string ) ) );.

Note: See TracTickets for help on using tickets.