1

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="<?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="<?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="<?php the_permalink(); ?>">
                        <?php
                        if ( has_post_thumbnail() ) {
                        ?>
                            <img class="mega-menu-projects-img" src="<?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>
2

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.

| improve this answer | |
  • I'm also showing these custom post type in the mega menu (header) but obviously, now just 6 posts are shown there (not just on the archive page). Is there a way to prevent it and just use this function on the Archive page? Thanks a lot – Crashy Feb 5 '18 at 12:53
  • I just added in my answer the code which I have header.php – Crashy Feb 5 '18 at 12:55
  • 1
    is_main_query will probably be what you're looking for. It's only true for the main query that WP will execute to find the post(s) that the request is asking for, but not for consecutive queries (as a custom query in your header). I've edited my answer to include it. – janh Feb 5 '18 at 12:57
  • is_archive('projects-ie') doesn't accept parameters, use is_post_type_archive('projects-ie') instead – Mike Szostech Feb 11 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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