It’s quite difficult because you are asking SQL to parse a string in order to determine if a numeric value is within this range. It would be much simpler if your meta data was separated by beginning and ending periods. Each as its own meta value. Then your query could very easily determine if the number representing now was less than the end AND more than the beginning.
I don’t know how far you are along in establishing meta data for all the flowers, but this is an important enough distinction that it would be worth writing a script to transform the data like I mentioned even if the data is already in place for thousands of flowers. Even better yet, instead of listing month.day numbers, store the meta data as a true real number, so if the start date were 03.15, actually store the result of 3 + 15/31 as 3.48.
This is obviously more difficult for humans to enter, but a bit of PHP could take a human entry like 3-15 and convert it for us to 3.48.
Thread Starter
McF
(@mcf)
Thanks for the answer.
I followed your suggestion.
Added two meta key – one for start, another for the end of flowering period.
I made them as integer values – days from beginning of the year.
global $post; // declare global post variable
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
// Period start
array(
'key' => 'Flowering 1 start',
'value' => $this->get_year_days(),
'type' => 'numeric',
'compare' => '<='
),
// Period end
array(
'key' => 'Flowering 2 finish',
'value' => $this->get_year_days(),
'type' => 'numeric',
'compare' => '>='
)
)
);
// get the latest blog entry
return get_posts( $args ); // and more stuff here
-
This reply was modified 9 years, 2 months ago by
McF.
Very nice! I had one related thought you didn’t ask about, so feel free to ignore. As I’m sure you know, flowering periods can run early or late, depending on the year’s weather. Unless this variance is already built into your data, you might consider maintaining a seasonal adjustment factor of plus or minus so many days. This could be saved in options by using the Settings API to add a field somewhere. Your query code can add this value to the start and finish values of the query.
Just a thought. It may not even apply to what you’re doing. It would require someone keeping this up to date, so may be a terrible idea if that might not happen.
Thread Starter
McF
(@mcf)
Thanks for the answer.
My wife gave me flowering periods. If they aren’t very precise (starting on June), then I will set first of June as starting period – that’s it. It’s not possible to adjust periods for each year…