too long

I've coded a custom archive display for a Wordpress-based blog based on the excellent code found HERE.

Basically, what it does:

Custom archive image from www.wpbeginner.com

But I need to find a way of filtering out of the archive certain posts that fall within categories that should not be archiveable. For example, if a category "Misc. Ideas" is holds entries that are not really posts, then the blog's owner doesn't want them to be available in the post archive.

I've been looking all over and can't find something that works for me. I think something in THIS source may help me but I'm testing stuff out and can't get something to work for me. So I turn to you guys. Maybe you've got an idea...

The PHP code I've right now is pretty straightforward. Obviously, the issue is filtering posts out in the query itself but I can't seem to find a query that works for me.

Plus/minus minor tweaks I've made to it in order to suit specific needs, it's basically the code obtained from the first source give here:

global $wpdb;
$limit = 0;
if( is_date() ) {
    $year_now = get_the_time('Y');
} else {
    $year_now = date('Y'); 
}
$year_prev = null;
$months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY month , year ORDER BY post_date DESC");
foreach($months as $month) :
$year_current = $month->year;
if ($year_current != $year_prev){
    if ($year_prev != null){
        // Do nothing
    }

    /* BREAK PHP HERE FOR SOME HTML */
    <li class="archive-year"><a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/"><?php echo $month->year; ?></a></li>
    /* BACK ON REGULAR PHP */

}
if($year_current == $year_now) {

    /* BREAK PHP HERE FOR SOME HTML */
    <li class="archive-month"><a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>"><span class="archive-month"><?php echo date_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span></a></li>
    /* BACK ON REGULAR PHP */

}
$year_prev = $year_current;
endforeach;

Well, it seems like not very many people out there have this issue or are interested in anything similar. However, in following the nature of SO, if somebody ever comes around having a similar issue, they'll be happy to find I was able to solve it by using a number of things I found and sticking them together and 5 hours of trial and error, at the end of which I came up with the following query:

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) 
INNER JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.term_id != X AND $wpdb->term_taxonomy.parent != X
AND   $wpdb->term_taxonomy.term_id != Y AND $wpdb->term_taxonomy.parent != Y
AND   $wpdb->term_taxonomy.term_id != Z AND $wpdb->term_taxonomy.parent != Z
AND   $wpdb->term_taxonomy.taxonomy = 'category' 
AND   $wpdb->posts.post_status = 'publish' 
AND   $wpdb->posts.post_type = 'post' 
AND   post_date <= now( ) and post_type = 'post' GROUP BY month , year ORDER BY post_date DESC

Where X, Y, and Z are to be substituted by the category ID numbers. (You may find these by going to the Wp-admin section where you edit your categories and hovering over those categories you wish to be hidden from the custom archive; you'll notice the link URL has a &tag_ID=# portion, where the # is the category number.)

Well, that did it just fine. Hope it may help somebody out there!