i've created a custom loop for a custom post type in my project, that is sorted by a custom field (wether the field is filled or not).

So basically, on that archive page, all posts of that type should appear, first the ones that have the custom field with value "yes", then the ones with value "no".

This part is working as expected, however i'm struggling to make this archive page paginated, with about 8 results per page or so. i was able to get it to work to show the paging links, but all pages other than the first page show an error 404.

I've tried using some of the answers that i've found across the web, but most of them end up messing up my sorting, or showing all posts instead of only that custom type.

here is my code:

<?php $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
                    // get posts
                    $args = array(
                        'post_type'         => 'restaurante',
                        'posts_per_page'    => 4,
                        'meta_key'          => 'destacado',
                        'orderby'           => 'meta_value',
                        'order'             => 'DESC'

                    $the_query = new WP_Query( $args );

                     if( $the_query->have_posts() ): ?>
                        <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>


                            $big = 999999999; // need an unlikely integer

                            echo paginate_links( array(
                                'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                                'format' => '?paged=%#%',
                                'current' => max( 1, get_query_var('paged') ),
                                'total' => $the_query->max_num_pages
                            ) );

                    <?php endif; ?>

                    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Can anyone please point me in the right direction?

Thanks in advance

1 Answer 1


You shouldn't be using a custom query for a post type archive page. Pagination being difficult is one of the reasons. If you want to change your post type archive to sort by a custom field, hook into pre_get_posts to modify the main query:

function wpse_282839_post_type_order( $query ) {
    if ( ! is_admin() && $query->is_post_type_archive( 'restaurante' ) ) {
        $query->set( 'posts_per_page', 4 );
        $query->set( 'meta_key', 'destacado' );
        $query->set( 'orderby', 'meta_value' );
        $query->set( 'order', 'DESC' );
add_action( 'pre_get_posts', 'wpse_282839_post_type_order' );

Then just use the normal loop and pagination links in your archive template.

  • Thank you Jacob, worked like a charm! I my ignorance i was afraid of modifying the main query, worrying that it would change the other custom post type pages! Commented Oct 16, 2017 at 10:49

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.