Some background:
- I'm trying to replace the default main query for archive templates, and to return a subset of a custom post type ('days') where meta values against the key 'dates' is between a certain value
- The 'days' custom post type has a custom variable field of 'dates' which stores individual database rows in the standard wp_postmeta table for each date associated with that post, stored in Epoch format ('U')
- I'm fairly confident that the SQL query is functioning correctly; the post IDs it returns appear to be correct in each case for the date range queried.
I'm running the following query in my functions file:
function modify_queries( $query ) {
global $wpdb;
if ( $query->is_post_type_archive('days') && $query->is_main_query() ) {
// Get Days with a date within the archive range
$year = substr($query->query_vars['m'],0,4);
$month = substr($query->query_vars['m'],4,2);
$day = substr($query->query_vars['m'],6,2);
if (is_year()) {
$startDate = date('U', mktime(0, 0, 0, 1, 1, $year));
$endDate = date('U', mktime(0, 0, 0, 12, 31, $year));
}
if (is_month()) {
$startDate = date('U', mktime(0, 0, 0, $month, 1, $year));
$endDate = date('U', mktime(0, 0, 0, $month, cal_days_in_month(CAL_GREGORIAN, $month, $year), $year));
}
if (is_day()) {
$startDate = date('U', mktime(0, 0, 0, $month, $day, $year));
$endDate = date('U', mktime(0, 0, 0, $month, $day, $year));
}
$request = "SELECT ID FROM $wpdb->posts, $wpdb->postmeta";
$request .= " WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";
$request .= " AND post_status='publish' AND post_type='days'";
$request .= " AND $wpdb->postmeta.meta_key = 'dates' AND $wpdb->postmeta.meta_value >= $startDate AND $wpdb->postmeta.meta_value <= $endDate LIMIT 0 , 30";
$postsList = $wpdb->get_results($request,ARRAY_N);
if ($postsList) {
foreach ($postsList as $thePost) {
$thePostList[] = $thePost[0];
}
$query->set( 'post__in', $thePostList);
$query->set( 'post_type', 'days');
return;
} else {
return false;
}
}
add_action( 'pre_get_posts', 'modify_queries',1 );
My understanding is that this should take the output of the query and simply replace the normal query with one using the IDs returned from the SQL query - however, it's just returning the 5 most recent 'days' posts.
At present, the archive.php file consists purely of the following, so we can rule out any interference:
<?
while ( have_posts() ) : the_post();
global $post;
the_title();
endwhile;
?>
I get the 5 most recently published 'days' posts, and it seems to ignore the IDs that the pre_get_posts filter should have passed it.
I have a sneaky suspicion that part of the problem relates to setting 'post_id' on the main query, rather than 'p', but I don't seem to get any results at all with the latter.
I've hunted around, but can't find much documentation about pre_get_posts other than how useful and beneficial it is as a replacement to query_posts.
Live debug output at: http://dev.daysoftheyear.com/days/2012/10