Skip to content

Commit

Permalink
Fix excluded categories in latest-posts (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
albig authored Aug 8, 2024
1 parent bd680af commit c1d61b8
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions functions/latest-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,43 @@
* @return WP_Query
*/
function sunflower_get_latest_posts( $number = -1, $category_ids = null, $exclude_ids = null ) {
$tax_query = null;
$tax_query = array();

if ( $category_ids || $exclude_ids ) {
if ( $exclude_ids ) {
array_push(
$tax_query,
array(
'taxonomy' => 'category',
'field' => 'slug',
'operator' => 'NOT IN',
'terms' => $exclude_ids,
)
);
}

if ( $category_ids ) {
// In sunflower < 2.1.0, category_ids is a comma separated string.
if ( ! is_array( $category_ids ) ) {
$category_ids = explode( ',', $category_ids );
}

if ( sunflower_is_numeric_array( $category_ids ) ) {
$tax_query = array(
array_push(
$tax_query,
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $category_ids,
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'operator' => 'NOT IN',
'terms' => $exclude_ids,
),
)
);
} else {
$tax_query = array(
array_push(
$tax_query,
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $category_ids,
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'operator' => 'NOT IN',
'terms' => $exclude_ids,
),
)
);
}
}
Expand Down

0 comments on commit c1d61b8

Please sign in to comment.