Exclude certain post formats from pagination

I have the standard blog post format and gallery format. I have excluded all the gallery posts from the index.php page so only standard posts show up there. However, the gallery format posts are still being counted for pagination and there is just a blank space on the pagination pages where there would be a gallery post format. How can those be excluded from pagination?

 ?php echo paginate_links(); ?

I tried this but it didn't help:

?php
$args = array(
    'post_type' = 'post',
    'tax_query' = array(
        array(
            'taxonomy' = 'post_format',
            'field' = 'slug',
            'terms' = array(
                'post-format-gallery'
            ) ,
            'operator' = 'NOT IN',
        ) ,
    )
);
?
 ?php echo paginate_links($args); ?

This is the query that displays the standard blog posts:

    ?php if (have_posts() ): ?
    ?php while (have_posts() ): the_post(); ?
     ?php $format = get_post_format( $post_id ); ?
   ?php if($format != 'gallery'): ?
    div class="blog" style="width: 350px"a href="?php esc_url(the_permalink()); ?" class="w-inline-block"?php the_post_thumbnail(); ?/a
        div class="gallery-info"a href="?php esc_url(the_permalink()); ?" class="linkgallery"?php the_title(); ?/a/div
        ?php $the_category = get_the_category(); ?
            div class="gallery-info gallery-category"?php foreach($the_category as $category): ??php echo $category-cat_name . ' '; ?
        ?php endforeach; ?
        /div
    /div
?php endif; ?
    ?php endwhile; ?
    ?php wp_reset_postdata(); ?
    ?php endif; ?

Topic post-formats pagination Wordpress

Category Web


As @SallyCJ mentioned in the commentary, you can change the query by filter pre_get_posts. There is no need to perform another WP_Query.

add_action('pre_get_posts', 'se336587_exclude_gallery_format');
function se336587_exclude_gallery_format( $query )
{
    if ( $query->is_main_query() /* here is additional condition (see below) */ )
    {
        $query->query_vars['tax_query'][] = [
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => ['post-format-gallery'],
            'operator' => 'NOT IN',
        ];
    }
}

IF additional condition

  • && is_home() - the filter will be applied on the homepage,
  • && is_category() - the posts on the category page will be filtered,
  • && (is_home() || is_category()) - in both of the above-mentioned places.

You are getting into a bit of a mess by simply skipping posts in the loop if they are not the correct format. You have a couple of options.

Firstly, you can perform the filter using a hook in your theme functions.php. Have a look at pre_get_posts, which allows you to alter the query and remove these gallery posts. Then, in your template you can use the loop and pagination as normal. You can use a check for a specific page or template when applying this hook.

Second option is to call query_posts() in your template or create a new WP_Query to override the main query. You will need to use get_query_var( 'paged') to get the current pagination, for example:

<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'paged'     => $paged,
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => array(
                'post-format-gallery'
            ),
            'operator' => 'NOT IN',
        )
    )
);

$query = new WP_Query($args);
?>

<?php if ($query->have_posts()): ?>
    <?php while ($query->have_posts()): ?>
        <?php $query->the_post(); ?>
        // Loop
    <?php endwhile; ?>

    <?php echo paginate_links(array(
        'total'   => $query->max_num_pages,
        'current' => $paged
    )); ?>

<?php endif; ?>

<?php wp_reset_postdata(); ?>

Un-tested code but that should get you started.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.