I am working on a project which has a functionality of “featured_posts” based on this discussion. I have created a table which lists all the posts. I have a checkbox in each row which upon click submits the for through javascript. when the box is checked, it updates everything and marks the post as featured.
As a test, i disabled all the featured posts. Upon unchecking the last checkbox, i got
Warning: Invalid argument supplied
warning. My code is
foreach( $_POST['list_id'] as $listID ) {
if( update_post_meta( $listID, '_property_featured', 'true' ) ) {
$featureMessage = "Selected listings have been marked as featured successfully.";
}
else $featureError = "An error occurred. Please try again.";
}
list_id refers to the post_id and was in the value attribute of checkbox. I know when the last checkbox was unchecked, i had to face this warning. Question is how can I track unchecking of checkbox as it is not included in the POST data of the form.
This appears to be a general PHP warning, you’re supplying an incorrect argument in a
foreach
loop.$_POST['list_id']
is expected to be an array or any other (iteratable object), when no checkboxes are checked I assume$_POST['list_id']
is completely undefined.Adding this before you iterate over everything should help you get rid of the warning altogether.
Never assume anything about
$_POST
. It is user input and could contain anything. First check if$_POST['list_id']
exists, usingisset()
orempty()
. Then make sure it is an array. You may simply cast it to an array if you wish. Finally, make sure the array values are actually absolute integers.All post IDs not found in the submitted list should be unfeatured.