I’d like to show 4 related posts (without a plugin) at the bottom of my blog. However, I want to exclude a certain category.
For example, if my blog post is in category 2
and 3
, I want to ignore category 2
and only look for blog posts of category 3
. Here is the relevant part of single.php
:
Note: my code below is currently not working.
$related = get_posts( array(
'category__in' => wp_get_post_categories( $post->ID ),
'numberposts' => 4,
'orderby' => 'date',
'post__not_in' => array( $post->ID ),
'cat' => '-2'
) );
if( $related ) {
foreach( $related as $post ) {
setup_postdata($post);
/** .. **/
}
}
Update: Category 2
is so prevalent that I want to ignore it in the search, but not hide those results.
For example, this post is in category 2
and 3
. I want to find other posts with category 3
, and maybe they have category 2
, but I only want to search by category 3
.
Update 2: I have this code below and I believe it is now working correctly:
$cat_ids = get_the_category();
if( ! empty( $cat_ids ) ) {
$post_cat_ids = array();
foreach( $cat_ids as $cat_id ) {
if( $cat_id->cat_ID != 2 ) {
$post_cat_ids[] = $cat_id->cat_ID;
}
}
}
$related = get_posts( array(
'category__in' => wp_get_post_categories( $post->ID ),
'numberposts' => 4,
'orderby' => 'date',
'exclude' => array( $post->ID ),
'category' => $post_cat_ids
) );
What I could gather from your question is:
-
You want to ignore one category (may be more) in the related post query.
-
However, you don’t want to actually exclude the posts from that category (in case any post belongs to that category, but also belongs to another category you want to look for).
Based on the assumption above, you may use the following CODE (some explanation is given within the CODE in comments):
// set the category ID (or multiple category IDs)
// you want to ignore in the following array
$cats_to_ignore = array( 2 );
$categories = wp_get_post_categories( get_the_ID() );
$category_in = array_diff( $categories, $cats_to_ignore );
// ignore only if we have any category left after ignoring
if( count( $category_in ) == 0 ) {
$category_in = $categories;
}
$cat_args = array(
'category__in' => $category_in,
'posts_per_page' => 4,
'orderby' => 'date',
'post__not_in' => array( get_the_ID() )
);
$cat_query = new WP_Query( $cat_args );
while ( $cat_query->have_posts() ) : $cat_query->the_post();
/* just example markup for related posts */
echo '<h2><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h2>';
endwhile;
// reset $post after custom loop ends (if you need the main loop after this point)
wp_reset_postdata();
You cannot use 'cat' => '-2'
or 'category__not_in' => array(2)
because that will exclude all posts having category 2
, even if those posts have other categories as well. So, instead of excluding, I’ve ignored the category 2
in the query with this CODE: array_diff( $categories, $cats_to_ignore );
.
Note: I’ve used WP_Query
instead of get_posts()
because iteration with WP_Query
looks more like the original loop. But of course you can use the get_posts()
function as well, as it calls WP_Query
internally anyway.