WordPress中get_day_link()无法精准生成日归档链接的问题
get_day_link() Issues with Custom Post Types in WordPress Hey there, let's walk through why you're running into these problems with get_day_link() and how to fix them properly.
First, let's break down the root causes of your two issues:
1. 404 Errors When Using get_day_link() Inside the Loop
The core problem here is that WordPress' default date archive pages only include the post post type by default. When you're working with custom post types, those archives don't automatically pull in your CPT content—so even though the link looks correct, there's no content to display, hence the 404.
2. Fixed Date (19 Instead of 20) When Defining Variables Outside the Loop
get_the_time() relies on the global $post object to fetch the current post's date. When you define $archive_year, $archive_month, and $archive_day outside the loop, you're grabbing the date of the first post in your query result. All subsequent links in the loop will reuse that initial date, which is why it's stuck at 19.
Step-by-Step Solutions
1. Make Date Archives Support Your Custom Post Type
First, we need to tell WordPress to include your custom post type in date archive queries. Add this code to your theme's functions.php file (or a custom plugin):
function include_cpt_in_date_archives( $query ) { // Target front-end main date archive queries only if ( is_date() && !is_admin() && $query->is_main_query() ) { // Replace 'your_custom_post_type' with your CPT's slug $query->set( 'post_type', array( 'post', 'your_custom_post_type' ) ); } } add_action( 'pre_get_posts', 'include_cpt_in_date_archives' );
This modifies the main query to pull in both default posts and your custom post type on date archives (day, month, year).
2. Correctly Generate Day Links Inside the Loop
Now, update your loop code to fetch the current post's date inside the loop, and always escape the URL for security:
<?php if( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> <a href="<?php echo esc_url( get_day_link( get_the_time('Y'), get_the_time('m'), get_the_time('d') ) ); ?>"> <?php echo get_the_date(); ?> </a> <?php endwhile; wp_reset_postdata(); endif;?>
3. Fix the "This Day’s Posts" Link (If You Need That Version)
If you want to keep the "This day’s posts" link, move the date variable definitions inside the loop so they update for each post:
<?php if( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); // Fetch current post's date inside the loop $archive_year = get_the_time( 'Y' ); $archive_month = get_the_time( 'm' ); $archive_day = get_the_time( 'd' ); ?> <a href="<?php echo esc_url( get_day_link( $archive_year, $archive_month, $archive_day ) ); ?>"> <?php _e( 'This day’s posts', 'textdomain' ); ?> </a> <?php endwhile; wp_reset_postdata(); endif;?>
Don't forget wp_reset_postdata() at the end of the loop—it resets the global $post object to its original state, preventing conflicts with other queries on the page.
Why Did Month/Year Archives Work But Day Archives Didn’t?
Chances are, your month/year archives had existing default post content to display, so you didn't notice the 404 issue. Day archives likely had no default posts, so the lack of custom post type content became obvious. The fix above ensures all date archives (day, month, year) include your CPT.
内容的提问来源于stack exchange,提问作者Hakik Zaman

