I was conflicted on whether to post this on WP Answers or here because I’m talking about wp_Queries, but I think this is more of a general PHP question, so here goes.
I am building an event calendar function in a project. What I have so far can be seen here, if you need a visual: http://theseattlevine.com/wordpress2014/calendar/?m=Feb&y=2014
The number of days (represented by squares, like a normal calender) is generated using query args that communicate with the date() and strtotime() functions which generates the proper amount of days in the month and starts the month on the proper day (Mon-Sun). I am using a for loop to generate the squares a finite numbers of times based on the number of days in each month.
Now, inside each square I want to, obviously, have a list of events happening on that day. The most logical way in my brain to do this is to have a query loop in each square that finds all the events with dates falling between 12:00am and 11:59pm on that day, then just echo out the title of each one. But, it doesn’t seem like this type of query works inside a for loop, and the research that I’ve done leads me to believe putting a loop inside a for loop is a bad idea because it creates a large number or requests on the server.
My question, finally, is what is the best method to do this then? How would I query posts from a database (like wp_query in WordPress) inside of each occurrence of a for loop?
I would try to do it differently.. Throwing 30xn requests on one page, and have m users viewing that page, your Database would eventually cry.
How about this?
Get all events in the month from SQL, put them into an array like this:
$event[$dayofmonth][title]=$title
Get another array in the form like
$date[$weeknumber][$dayofweek]=$dayofmonth
to help building the calendar frameThen when you iterate through the $date to build calendar frames, you have $dayofmonth at your disposal to bring in the event title, or more details if you built that into the event array.
This way you only need 1 query ( a good chance is you can use just php to build that $date array.
Sample code as follows:
If you make another table which stores data in the following format:
Date || Event Name(s)
then it will be very easy to just query for the specific date from this table.