0

Hi I have a main category which is "Anime" and three sub-categories which is "On going" - "Completed" - "Future" ( also there's other categories too ). I'm using this code to show related posts on single.php:

<div class="sixteen wide column recommendationss" style="display:none;padding-top: 0px;">
    <div class="ui master table">
        <div class="body two-cells">
<?php
    $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );
    if( $related ) foreach( $related as $post ) {
    setup_postdata($post); ?>
            <div class="row">
                <div class="cell cover">
                    <div class="ui lazy loaded" style="padding-bottom: 145.33%;">
                        <?php the_post_thumbnail();?>
                    </div>
                </div> 
                <div class="cell info">
                    <a href="<?php the_permalink() ?>"><?php the_title(); ?></a> 
                    <small><?php the_field('archivyear'); ?></small>
                </div>
            </div>
    <?php }
    wp_reset_postdata(); ?>
        </div>
    </div>
</div>

so how can I remove those four categories from this query?

1 Answer 1

0

You can use the category__not_in parameter to exlude categories.

get_posts uses the WP_QUERY parameters

Your code will be :


$related = get_posts( array( 
               'category__in' => wp_get_post_categories($post->ID),
               'numberposts' => 5, 
               'post__not_in' => array($post->ID),
               'category__not_in' => array(1,2,3,4)'
          ) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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