I assume this is due to a limit on the length of the array for
post__not_in. Does anyone know what that is please?
I don't believe so, a wrote rudimentary test on a site with 1700+ posts+pages to test this case, and it reveals no duplicates ever returned.
Here is my code:
add_action('wp_footer', function() {
$allPosts = get_posts([
'post_status' => 'publish',
'post_type' => ['post', 'page'],
'posts_per_page' => -1,
'orderby' => 'rand'
]);
$totalPostCount = count($allPosts);
$duplicatesFound = false;
$loadedIds = [];
while (!$duplicatesFound && count($loadedIds) < $totalPostCount) {
$args = array(
'post_status' => 'publish',
'post_type' => array('post', 'page'),
'post__not_in' => $loadedIds,
'posts_per_page' => 100,
'orderby' => 'rand',
);
$posts = get_posts($args);
$postIds = wp_list_pluck($posts, 'ID');
$loadedIds = array_merge($loadedIds, $postIds);
$uniqueIds = array_unique($loadedIds);
if (count($uniqueIds) < count($loadedIds)) {
$duplicatesFound = true;
}
}
var_dump($duplicatesFound);
var_dump($totalPostCount);
var_dump(count($loadedIds));
var_dump(count($uniqueIds));
var_dump($loadedIds);
var_dump($uniqueIds);
die();
});
If you have more than 1700 posts, it's possible that it wasn't enough to reproduce the issue in my test, in which case i'd suggest running the above code yourself to be certain.
Assuming you have already verified that your $_POST["LoadedIDs"]
contains a valid array of ID's (not a comma separated string).
My best guess is that your data is getting lost/cut short in POST, to verify this you could try Joal M's solution. If that doesn't solve your problem this will confirm that your data is being lost in POST.
To fix this you could try increasing some ini settings:
max_post_size = 100M
max_input_vars = 3000
suhosin.post.max_vars = 3000
suhosin.request.max_vars = 3000
max_input_vars
is most likely the culprit as this is limited to 1000 by default.
The other alternative is to not pass the ID's back from the client but instead store them on the server perhaps in session as Joal M also suggested, but i'd save this for a final resort.