• Hi,
    Is it possible to create a date archive for a specific category? I have a site where I would like the date archive in the sidebar to show the list of posts for the current category.

    My site has several displays by category.

    I would like to do the same for the tag cloud.

    Are there any smart plugins available or is this custom code?

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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.

    Thread Starter Tim Burkart

    (@bigmoxy)

    Hi @faisalahammad,

    Thank you for your reply. Unfortunately the plugin you referenced has not been updated for over 2 years.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Date Archives by Category’ is closed to new replies.