user930026
user930026

Reputation: 1657

Wordpress Post__not_in not working when supplied through variable

I have created a function and passing dynamically the comma separated values to wp_args. When i echo variable i get the exact as needed and passing statci gies result but while passing variable name, I do not get results.

$excludepages1 = "12,14";
$excludepages2 = "'".implode("','", explode(",", $excludepages1))."'";
$excludepages = str_replace('"', '', $excludepages2); 

Now If i echo $excludepages I get '12','14' But when i pass here

$children = get_posts( array(
        'post_type' =>'page',
        'posts_per_page' =>-1,
        'post_parent' => $post_id,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post__not_in' => array($excludepages)));

I do not get any result and if instead of variable i pass '12','14', I get results, Can you please help?

Upvotes: 1

Views: 2070

Answers (3)

тнє Sufi
тнє Sufi

Reputation: 634

Problem is not with the post__not_in argument. It takes array, not a comma separated string:

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

More details: https://developer.wordpress.org/reference/classes/wp_query/parse_query/

Your $excludepages returns string(9) "'12','14'".

You should update it like:

$excludepages1 = "12,14";
$excludepages = explode(",", $excludepages1);

$children = get_posts( array(
    'post_type' =>'page',
    'posts_per_page' =>-1,
    'post_parent' => $post_id,
    'orderby' => 'menu_order',
    'order' => 'ASC',
    'post__not_in' => $excludepages)
);

In above code, $excludepages will return:

array(2) {
  [0]=> string(2) "12"
  [1]=> string(2) "14"
}

Upvotes: 1

Lucas Healy
Lucas Healy

Reputation: 133

Doing it the way you are you are creating an array with a 0 index and value of "12,14." What you are doing is passing a string "12, 14" to the first index of an array. What you want to do is pass two integers into an array. So if you print array($excludepages) the way you're doing it you'll see

Array
(
    [0] => 12,14
)

What you want is

Array
(
    [0] => 12
    [1] => 14,
)

I'm not sure what you're trying to do with the implodes, explodes, and str_replace, but you'll want to define your array using something like:

$excludepages = array(12, 14); // Notice, no quotes in the array declaration.

or

$excludepages = array();
$excludepages[] = 12;
$excludepages[] = 14;

Then in the get_posts it would look like:

$children = get_posts( array(
        'post_type' =>'page',
        'posts_per_page' =>-1,
        'post_parent' => $post_id,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post__not_in' => $excludepages) );

Upvotes: 1

Tamilvanan
Tamilvanan

Reputation: 718

Ref: https://developer.wordpress.org/reference/functions/get_posts/

$excludepages1 = "12,14";
$excludepages = explode(",", $excludepages1);

An exploded array can be used directly.

"exclude" param is suggested in the docs. So, I used instead of "post__not_in".

$children = get_posts( array(
                'post_type' =>'page',
                'posts_per_page' =>-1,
                'post_parent' => $post_id,
                'orderby' => 'menu_order',
                'order' => 'ASC',
                'exclude' => $excludepages)
            );

Upvotes: 1

Related Questions