Change number of posts to show on Archive page (custom post type)

I’ve created a custom post type, and I’ve created an archive page to show them on this page.

The number of posts shown by default is 10 but I’d like to reduce the posts’ number to 6.

I use this function to reduce the number of posts:

function num_posts_archive_project_ie($query){
    if ($query->is_archive('projects-ie')) {
            $query->set('posts_per_page', 6);
   }
    return $query;
}

add_filter('pre_get_posts', 'num_posts_archive_project_ie');

This function seemed to work but when I went to the back office to add new projects (custom post type) I noticed that even in the back office the maximum of posts shown are 6.

On the archive page, I also put this code, which allows me to create more pages in the archive but to show just 6 posts per page:

<div class="container projects-wrap-projects-page">
    <div class="row">

        <?php
            if(have_posts()) : while(have_posts()) : the_post();
                $categorythumb = get_field( "category_hover_thumb_project");
        ?>

        <div class="col-sm-12 col-md-4 col-lg-4 col-xl-4 projects-container-1">
            <div class="projects-container-projects-page">
                <?php
                if ( has_post_thumbnail() ) {
                ?>
                <img class="projects-thumb-projects-page" src="https://wordpress.stackexchange.com/questions/293185/<?php the_post_thumbnail_url(); ?>" alt="">
                <?php
                }
                ?>
                <div class="overlay-white-projects">
                    <div class="text-thumb-projects-wrap">
                        <span><?php echo $categorythumb; ?></span>
                    </div>
                </div>
            </div>
            <div class="name-of-the-project">
                <span class="header-2"><?php the_title(); ?></span>
            </div>
            <div class="read-more-project">
                <a class="read-more-project-btn" href="https://wordpress.stackexchange.com/questions/293185/<?php the_permalink(); ?>">Read more</a>
            </div>
        </div>

        <?php
            endwhile; endif;
        ?>

    </div>

    <div class="row">
    <div class="col-12 text-center">
    <?php

        the_posts_pagination( array(
            'mid_size'  => 2,
            'prev_text' => 'Previous',
            'next_text' => 'Next',
        ) );

    ?>
    </div>
  </div>


</div>

How can I reduce the number of posts on the front end (archive page) but not reduce the number of posts that I can add in the back office?

I mean, the number of post in the back office shouldn’t be limited because on the archive page a shown more than 6 products, are just split on different pages.

Code in Header.php

<div id="mega-menu-projects-ie" class="mega-menu-projects-wrap" style="display: none;">
    <div class="mega-menu-prjects-row-1">
        <?php
        $args = array( 'post_type' => 'projects-ie', 'posts_per_page' => -1 );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();
        $mega_menu = get_field( "mega_menu");
        if (!empty($mega_menu)) {

        $projectlocationmenu = get_field( "project-location");
        ?>
        <div class="mega-menu-projects-container">
            <div>
                <div class="mega-menu-proj-title-container"><span>

                    <?php


                    $terms = get_the_terms( $post->ID , 'projects_ie_categories_register' );

                    foreach ( $terms as $term ) {

                    echo $term->name;

                    }

                    ?>

                </span></div>
                <div class="mega-menu-projects-img-container">
                    <a href="https://wordpress.stackexchange.com/questions/293185/<?php the_permalink(); ?>">
                        <?php
                        if ( has_post_thumbnail() ) {
                        ?>
                            <img class="mega-menu-projects-img" src="https://wordpress.stackexchange.com/questions/293185/<?php the_post_thumbnail_url(); ?>" alt="...">
                        <?php
                        }
                        ?>
                    </a>
                </div>
                <div class="mega-menu-proj-name-container"><span class="small-text"><?php the_title(); ?></span></div>
                <div class="mega-menu-proj-country-container"><span class="small-text"><?php echo $projectlocationmenu; ?></span></div>
            </div>
        </div>
        <?
    }
        endwhile;
        wp_reset_query();
        ?>
    </div>

1 Answer
1

Just make sure you’re not in the admin when changing the query:

function num_posts_archive_project_ie($query) {
    if (!is_admin() && $query->is_archive('projects-ie') && $query->is_main_query()) {
            $query->set('posts_per_page', 6);
   }
    return $query;
}

is_admin() does return true only if you’re in the backend (it does not, however, verify that the current user is an admin user, despite the name, don’t let that confuse you), so that hook won’t change anything in the backend post list.

Leave a Comment