I believe you are using the following code from this pastebin link
function posts_by_year() {
// array to use for results
$years = array();
// get posts from WP
$posts = get_posts(array(
'numberposts' => -1,
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'my-custom-post-type',
'post_status' => 'publish'
));
// loop through posts, populating $years arrays
foreach($posts as $post) {
$years[date('Y', strtotime($post->post_date))][] = $post;
}
// reverse sort by year
krsort($years);
return $years;
}
This function uses get_posts
to return the list of posts. To exclude a category, you can pass the category
parameter to your arguments. Just one note
Note: The category parameter needs to be the ID of the category, and not the category name.
Note: The category parameter can be a comma separated list of categories, as the
get_posts()
function passes the ‘category’ parameter directly intoWP_Query
ascat
.
EDIT
Please note, to exclude a category, you need to use the minus (-
) sign in front of the category ID. Something like this will do. Just change -13
to the -ID
where ID is the ID of the category you want to exclude
function posts_by_year() {
// array to use for results
$years = array();
// get posts from WP
$posts = get_posts(array(
'numberposts' => -1,
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'my-custom-post-type',
'post_status' => 'publish',
'category' => -13
));
// loop through posts, populating $years arrays
foreach($posts as $post) {
$years[date('Y', strtotime($post->post_date))][] = $post;
}
// reverse sort by year
krsort($years);
return $years;
}
EDIT 2
Copy and paste this code as is. You had a syntax error, that is why you got the blank page
function posts_by_year() {
// array to use for results
$years = array();
// get posts from WP
$posts = get_posts(array(
'numberposts' => -1,
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'project',
'post_status' => 'publish',
'category' => -27
));
// loop through posts, populating $years arrays
foreach($posts as $post) {
$years[date('Y', strtotime($post->post_date))][] = $post;
}
// reverse sort by year
krsort($years);
return $years;
}
EDIT 3
It seems that you are making use of a custom taxonomy. If this is the case, make use of tax_query
For available parameters, see WP_Query
. You just need to add the name of your taxonomy in the taxonomy
parameter
function posts_by_year() {
// array to use for results
$years = array();
// get posts from WP
$posts = get_posts(array(
'numberposts' => -1,
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'project',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'NAME OF YOUR CUSTOM TAXONOMY',
'field' => 'term_id',
'terms' => '27',
'operator' => 'NOT IN'
),
),
));
// loop through posts, populating $years arrays
foreach($posts as $post) {
$years[date('Y', strtotime($post->post_date))][] = $post;
}
// reverse sort by year
krsort($years);
return $years;
}