Hi @bigmoxy
Yes, it is possible to create a date archive for a specific category in WordPress. There are several ways to do this:
1. Using a plugin: Several WordPress plugins allow you to create custom date archives for specific categories. One such plugin is “Custom Post Type Date Archives“. This plugin allows you to create custom date archives for any post type, including categories.
2. Using custom code: You can also create a custom date archive for a specific category using custom code. To do this, you can use the pre_get_posts action hook to modify the main query before it is executed. You can use the is_date()
function to check if the current page is a date archive and then use the set()
method of the WP_Query
object to set the cat parameter to the desired category ID.
Here is an example of how you can use the pre_get_posts
action hook to create a custom date archive for a specific category:
function custom_date_archive_for_category( $query ) {
if ( $query->is_date() && $query->is_main_query() ) {
$query->set( 'cat', '5' ); // Replace '5' with the ID of the desired category
}
}
add_action( 'pre_get_posts', 'custom_date_archive_for_category' );
To display the list of posts in the sidebar, you can use the wp_get_archives()
function. This function generates a list of links to date archives, and you can use the type parameter to specify the type of archive to display (e.g. ‘monthly’, ‘yearly’). For example:
wp_get_archives( array( 'type' => 'monthly', 'cat' => '5' ) ); // Replace '5' with the ID of the desired category
To create a custom date archive for a specific tag, you can use a similar approach, using the tag parameter instead of the cat parameter.
I hope this helps! Let me know if you have any questions.