Is there any way to exclude a category from wp_get_archives? I’m trying to show the months in the sidebar, but I want to exclude the posts that are not blog entries.
$catID = get_cat_id('Projects');
$variable = wp_get_archives('type=monthly&show_post_count=1);
echo $variable;
Use this if you want to include only specific categories for wp_get_archive function in your functions.php of your theme directory
You can write a filter in your functions.php file which will change wp_get_archive function’s default behavior.
Ran into this problem in a project but never found a solution online — mine isn’t the prettiest PHP, but it does the trick.
This is a play off the filter suggested by Katie, which I ran across in few support forums as well. This goes in your
functions.php
:In the second function, swap
taxonomy-name
for the name of your actual custom taxonomy.All the IDs of terms in your custom taxonomy are captured in a string; the rest operates the same as the original function — only that list of categories from your custom taxonomy are included in the
wp_get_archives()
list. You can also tweak the code to exclude them as well (first example above).If you only want one instance of the
wp_get_archives()
list to do this, just skip the top two lines of code in yourfunctions.php
that apply the filters. Then, when you use thewp_get_archives()
tag, apply the filters before it, and remove them afterwards:wp_get_archives()
does not have a mechanism to exclude based on category — it’s purely for time-based archives (yearly, monthly, daily, weekly) or “every post” archives: postbypost or post by post ordered by post title.There are different ways to work with category archives: WordPress ⺠Support » Limit archives to category / date archives for category
I use Clean Archives Reloaded WordPress ⺠Clean Archives Reloaded « WordPress Plugins and exclude categories around line 200:
You might want to look in to get_categories and lean towards your own custom solution. While this may cost you a little more time and work; you will indeed get the upshot of having full control over what you’re trying to achieve.
Place the code below just after
This code is working for me already 🙂
Can you use a filter hook on pre_get_posts instead?
I know something like this works for is_author, is_home, and is_feed…
depends on whether you can do it for something like is_archive or is_monthly
You would drop that in a php file with a plugin header:
Then upload it to your Plugins directory and activate it.
There isn’t an official way of doing this. But i tried and tested a lot of code block and only this one worked for me.
Replace your_category_id with an original id of your post category and the code will work.