• cichecki

    (@cichecki)


    Hi Ashley,

    I’m trying to limit my archives to single category (“blog”) only but unfortunately I can’t make it. I was trying method from FAQ as well as exluding category (topic found here in support) but neither of these works.

    I don’t know if something changed due to updates or am I doing something wrong. Some help will be highly appreciated! Code I dropped into functions.php :

    add_filter('expanding_archives_get_posts', function(array $args) {
         $args['cat'] = blog; // Replace with ID of your category.
    
         return $args;
     });
    
     add_filter('expanding_archives_query', function(string $query) {
         $category = get_category(blog); // 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 = blog)
     WHERE post_status = 'publish'
       AND post_date <= now()
       AND post_type = 'post'
     GROUP BY month, year
     ORDER BY post_date DESC
         ";
     });

    There are still all posts from all categories visible.

    Thanks!

    Jakub

    The page I need help with: [log in to see the link]

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

    (@nosegraze)

    Hi Jakub,

    The problem is that you have to pass through the ID of the category. The ID is a number (e.g. 123). You’re passing in the name or slug of your category (blog). That’s why it isn’t working.

    If you go to Posts > Categories and edit your chosen category, the category ID appears in the URL immediately after &tag_ID=

    Here’s an example:

    https://examplewebsite.com/wp-admin/term.php?post_type=post&tag_ID=1&taxonomy=category&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory

    In that URL you can see &tag_ID=1 which indicates that the ID number is 1

    That is the value you want to use in the code snippet.

    Thread Starter cichecki

    (@cichecki)

    Huge thanks for quick reply! That works for now, just need to work out how to fix the numbering in post count ??

    That was pretty simple but I’m total noob in php and coding ??

    Thanks a lot again!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Limit to specific category not working’ is closed to new replies.