Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

We have an issue with our date archive in WordPress, where our daily archive (example address www.xxx.com/2013/01/09) is only generated when a post from the default post type is added. Any day where a default post type was not posted, results in a 404 not found.

If we go to a day where a default post type was posted, we have no problems seeing articles from all post types via this function (from another stackexchange thread)

function custom_post_date_archive($query) {
if ($query->is_date)
    $query->set( 'post_type', array( 'news', 'sports', 'life', 'opinion', 'culture','community' ) );
    remove_action( 'pre_get_posts', 'custom_post_date_archive' );
}
add_action('pre_get_posts', 'custom_post_date_archive');

We've also tried other functions where we generate a daily archive URL for each post type (for example www.xxx.com/news/2013/09/01), but that is not the URL format we would like.

Basically we would like a way to tell WP to generate the base daily archive URL when ANY post type is added.

This was working previously, however, when a separate rewrite function was added to the functions file, daily archive generation for all post types ceased. We've tried all types of rewrie flushing, to no avail (though the addresses do work, just that WP only thinks to generate www.xxx.com/2013/01/09 if a default post is made).

Is there a function that can tell WP to always generate that address whenever any post type is added? Or is there another way to flush the system?

share|improve this question
    
archive URLs aren't "generated" in the sense you are thinking. the date rewrite pattern exists, and an incoming request that matches that pattern is parsed into query vars, and the database is queried. when the query results in nothing being found the result is a 404. I would start by inspecting the global $wp_query in the 404 template to see the actual query sent to the database for a clue as to why you're getting a 404. –  Milo Mar 28 '13 at 3:37
    
That's a good bit of direction for debugging. I dumped what was contained in the global $wp_query on the 404 for a daily archive I was looking for. And this is the SQL query returned (note the lack of a post_type such as 'news'). SELECT SQL_CALC_FOUND_ROWS wpt_3_posts.ID FROM wpt_3_posts WHERE 1=1 AND YEAR(wpt_3_posts.post_date)='2013' AND MONTH(wpt_3_posts.post_date)='1' AND DAYOFMONTH(wpt_3_posts.post_date)='9' AND wpt_3_posts.post_type = 'post' AND (wpt_3_posts.post_status = 'publish' Is there any possible hook for changing that post_type to 'any'? –  Ashkas Mar 28 '13 at 3:54
    
it's odd that your function to add all post types does not seem to be working as expected. try modifying it by removing the remove_action line, and changing the if condition to if ($query->is_date() && $query->is_main_query()). –  Milo Mar 28 '13 at 4:00
add comment

1 Answer

Thanks to Milo I got some direction in debugging this. Basically only the post_type of 'post' was being used in the SQL query for the daily archive. We can hook into this with pre_get_posts (which I had used earlier by my function was too specific).

The function on this page, tells wordpress to include all post_types in its global $wp_query.

http://uncommoncontent.com/2010/10/12/custom-post-types-in-the-loop-using-pre_get_posts-refined

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.