• Resolved dyflo93

    (@dyflo93)


    Hi,

    First of all I love your plugin !

    I wanted to know if you can tell me if it is possible to edit the code to show only archives from a specific category.

    I already tried to add a filter to a specific category in the Month.php file (by adding it to the args) but it’s always showing all posts.

    Can you help me about that please ? Thanks !

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Ashley

    (@nosegraze)

    Hi there ??

    I think I need to make some adjustments to make this work more reliably. I’ll do that then get back to you with an example!

    Plugin Author Ashley

    (@nosegraze)

    I’ve just released version 2.0.2. Once you upgrade to that you can add the following code to a custom plugin or your theme’s functions.php file:

    add_filter('expanding_archives_get_posts', function(array $args) {
         $args['cat'] = 2; // Replace with ID of your category.
    
         return $args;
     });
    
     add_filter('expanding_archives_query', function(string $query) {
         $category = get_category(2); // Replace with ID of your category.
         if (! $category instanceof \WP_Term) {
             return $query;
         }
    
         global $wpdb;
    
         return "
     SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year, COUNT(id) as post_count
     FROM {$wpdb->posts}
              INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id AND {$wpdb->term_relationships}.term_taxonomy_id = 2)
     WHERE post_status = 'publish'
       AND post_date <= now()
       AND post_type = 'post'
     GROUP BY month, year
     ORDER BY post_date DESC
         ";
     });

    Note that there are two places in the code where you need to set your category ID. Look for this:

    // Replace with ID of your category.

    The examples use category ID 2. Replace the 2 with your own ID.

    Also note that the results may not update instantly as the query to retrieve the date periods is cached for one day. To force the query to re-run, delete this transient: expanding_archives_months

    You can either do that with PHP via: delete_transient('expanding_archives_months')

    Or install a plugin like Transients Manager, which will allow you to search for expanding_archives_months and delete it. (Then you can deactivate the plugin.)

    Thread Starter dyflo93

    (@dyflo93)

    Hi Ashley,

    First of all, thank you so much for your reactivity and thank you for the update, you’re awesome !

    Thank you for these explanations, I tested the code and indeed it works but I had to change the category at the line below as well where there is “term_taxonomy_id = 2”

    INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id AND {$wpdb->term_relationships}.term_taxonomy_id = 2)

    This problem is solved, but I have another problem, the retrieved list is from January but it’s showing “december” instead of “january” in every years, so there is a lag of 1 month on each months I don’t know why.

    In the code it’s showing data-month=”1″ but the display month is december. Is it maybe a problem with the timestamp ? For information I’m from France and the date format in my WordPress dashboard is “l j F Y”.

    Thanks again !

    Thread Starter dyflo93

    (@dyflo93)

    Hi Ashley,

    I finaly used an array with months with numbers and looped in using this->monthNumber to assign the correct month in the Month.php file.

    Thank you so much for your help !

    Have a nice day ^^

    Plugin Author Ashley

    (@nosegraze)

    Glad you got it sorted. ?? I’ll double check how I’m handling time zones. Might also be something I can adjust there.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Archive for a specified category’ is closed to new replies.