Better handle cases where foreach() and in_array() would be called over null arrays

These changes handle a couple of cases where foreach()es were being called without ensuring that the iterated items existed, as well as once case where in_array() could be called on a non-existant object, causing posts not to display.