I am trying to EXCLUDE a POST FORMAT (gallery) from the loop. I am able to successfully show just the post format without the other posts using similar tax_query
but when I try and exclude the post format with the tax_query
'operator' => 'NOT IN'
it does exclude that post format, but then it endlessly repeats and loops all of the other posts. I'm not sure what I'm doing wrong here. I only want each post to display once, but just not those with the post-format-gallery.
A page with ONLY the post format that successfully works:
<?php
get_header(); ?>
<div class="site-content clearfix">
<?php if (is_active_sidebar('sidebar1')) : ?>
<div class="main-column">
<?php endif; ?>
<?php if (have_posts()) :
while (have_posts()) : the_post(); ?>
<article class="page">
<h2><?php the_title(); ?></h2>
<?php $args = array(
'post_type'=> 'post',
'post_status' => 'publish',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-gallery' )
)
)
);
$galleryPosts = new WP_Query ( $args );
if ($galleryPosts->have_posts()) :
while ($galleryPosts->have_posts()) : $galleryPosts->the_post(); ?>
<div class="page portfolio">
<?php the_post_thumbnail(); ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
</div>
<?php endwhile;
else :
endif;
wp_reset_postdata();
?>
</article>
<?php endwhile;
else :
echo '<p>No content found</p>';
endif; ?>
<?php if (is_active_sidebar('sidebar1')) : ?>
</div>
<?php endif; ?>
<?php get_sidebar(); ?>
</div>
<?php get_footer();
?>
Using similar code, with the attempt to EXCLUDE the post format works, but then endlessly loops and repeats all other posts:
<article class="post">
<?php $args = array(
'post_type'=> 'post',
'post_status' => 'publish',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-gallery' ),
'operator' => 'NOT IN',
)
)
);
$noGalleryPosts = new WP_Query ( $args );
if($noGalleryPosts->have_posts()) :
while($noGalleryPosts->have_posts()) : $noGalleryPosts->the_post();
?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="post-info"><?php the_time('F jS, Y'); ?>
| by
<a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author(); ?></a>
| Posted in
<?php
$categories = get_the_category();
$separator = ", ";
$output = '';
if ($categories){
foreach ($categories as $category) {
$output .= '<a href="' . get_category_link($category->term_id) . '">' . $category->cat_name . '</a>' . $separator;
}
echo trim($output, $separator);
}
?></p>
<?php if (!is_single() ) { ?>
<P>
<?php echo get_the_excerpt(); ?>
<a href="<?php the_permalink(); ?>">Read more»</a>
</p>
<?php } elseif (is_single() ) {
the_post_thumbnail('banner-image');
the_content();
} else {
the_content();
} ?>
<?php endwhile;
else :
endif;
wp_reset_postdata();
?>
</article>
</div>