0

This script I use to show articles from the same category in my single post, excluded the actual shown post...how can I exclude category 1 of this script so, that he further shows artikels from the same category, but not from category 1 ?

<?php
// Default arguments
$args = array(
    'posts_per_page' => 6, // How many items to display
    'post__not_in'   => array( get_the_ID() ), // Exclude current post
    'no_found_rows'  => true, // We don't ned pagination so this speeds up the query
         'orderby'       => 'rand',

);

// Check for current post category and add tax_query to the query arguments
$cats = wp_get_post_terms( get_the_ID(), 'category' ); 
$cats_ids = array();  
foreach( $cats as $wpex_related_cat ) {
    $cats_ids[] = $wpex_related_cat->term_id; 
}
if ( ! empty( $cats_ids ) ) {
    $args['category__in'] = $cats_ids;
}

// Query posts
$wpex_query = new wp_query( $args );

// Loop through posts
foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>

<div class="weitere_interessante_artikel_container">
<div class="weitere-artikel-pic-wrapper">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('middle');?></a>
</div>
<div class="text_container">
<a href="<?php the_permalink() ?>"><span><?php echo str_replace(':', ':</span>', get_the_title()); ?></a>
</div>
</div>


<?php
// End loop
endforeach;

// Reset post data
wp_reset_postdata(); ?>

1 Answer 1

0

You can use "category__not_in" array parameter. Also, I've modified your code just a bit, you should use wordpress best practice which is "if -> while" to display the posts. Please try these code below as I haven't tested it yet.

<?php
// Default arguments
$args = array(
    'posts_per_page'    => 6, // How many items to display
    'post__not_in'      => array( get_the_ID() ), // Exclude current post
    'no_found_rows'     => true, // We don't ned pagination so this speeds up the query
    'orderby'           => 'rand',
    'category__not_in'  => array( 1 )

);

// Check for current post category and add tax_query to the query arguments
$cats = wp_get_post_terms( get_the_ID(), 'category' ); 
$cats_ids = array();  
foreach( $cats as $wpex_related_cat ) {
    $cats_ids[] = $wpex_related_cat->term_id; 
}
if ( ! empty( $cats_ids ) ) {
    $args['category__in'] = $cats_ids;
}

// Query posts
$wpex_query = new WP_Query( $args );

// Loop through posts
if( $wpex_query->have_posts() ) :
while( $wpex_query->have_posts() ) : $wpex_query->the_post(); ?>

<div class="weitere_interessante_artikel_container">
<div class="weitere-artikel-pic-wrapper">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('middle');?></a>
</div>
<div class="text_container">
<a href="<?php the_permalink() ?>"><span><?php echo str_replace(':', ':</span>', get_the_title()); ?></a>
</div>
</div>


<?php
// End loop
endwhile;

// Reset post data
wp_reset_postdata(); 

endif; ?>
1
  • Many thanks and big respect ! It is function perfectly !
    – RodneyHawk
    Oct 9, 2016 at 16:38

Your Answer

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

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