0

I am working with a different variation of code already posted here, which was intended to filter the default 'wordpress archive widget' to only show month results for a specific category.

I am trying to alter it to instead exclude posts assigned with a specific custom taxonomy.

That custom taxonomy term id is 139, which for testing purposes is attached to a post in March 2016 (which is also the only post in March 2016). So, I edited it as follows.

If I set the tt.term_id = '139', then the Archive widget only shows 'March 2016', which would be correct. But if I make it NOT EQUAL as shown below, it shows all months INCLUDING 'March 2016', which is the one it should be excluding.

I'm guessing the issue is with the join filter somehow, but not sure how to adjust it. Thanks for any hints.

add_filter( 'getarchives_join' , 'getarchives_join_filter');
function getarchives_join_filter( $join ) {
global $wpdb;
return $join . " INNER JOIN {$wpdb->term_relationships} tr ON ($wpdb->posts.ID = tr.object_id) INNER JOIN {$wpdb->term_taxonomy} tt ON  (tr.term_taxonomy_id = tt.term_taxonomy_id)";
}

add_filter( 'getarchives_where' , 'getarchives_where_filter');
function getarchives_where_filter( $where ) {
global $wpdb;
return $where . " AND tt.term_id != '139' ";
}
0

This would probably be best done using a custom WP_Query as opposed to a filter.

So say your site is about plants but you don't want this custom archive to show posts where you have your plant custom taxonomy set to weeds:

$args = array(
   'post_type' => 'post',
   'tax_query' => array(
      'taxonomy' => 'plants',
      'field' => 'slug',
      'terms' => 'weeds',
      'operator' => 'NOT_IN',
   ),
);

$myArchive = new WP_Query($args);

So what this code is doing is creating a new query for posts looking at the slug for the taxonomy of plants and then grabbing them all except those with a slug name of weeds and then you can use the loop to display them however you want in the widget.

Just remember to use wp_reset_postdata() after the last post is displayed in your widget to reset $post back to the default query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.