You want to rethink this code… to begin with, as the number of entries grow, looping them all is a bad idea. Consider adding a meta_query
argument to the WP_Query
so you are not looping through every single entry in the future, but limiting the database retrieval instead.
Mainly you want to get rid of the last else{}
wrapper and move the code inside of it to outside of the while
loop completely, or you will be creating a lot of false entries. You may not have noticed if you are only testing one email address. This may also mean adding another condition to only trigger all of this when a form has actually been submitted (eg. if (isset($_POST['entryEmail')) {
… (just above $loop
)
There are also some other bugs going on..!
…right at the top you need to move $entryCode
down to after $entryEmail
is defined…
…inside createEntry
you should be using add_post_meta
– not update_post_meta
which only works if the meta value already exists…
…no big deal but $day
should be set as 300 not “300”, an integer not string…
What to learn from this? Go over the logic of your code a few more times before expecting it to work. You will be unlikely to get this kind of detailed response in future (you got lucky) – as code review is not the purpose of a Q&A site. Instead you would generally have to narrow down to a specific bug that you cannot figure out having attempted far more thoroughly to work it out yourself – and this would show in the way you ask the question.