Seen a lot of people with same issue, but no solutions have worked for me, so maybe I'm mislooking something. Page 2 of a custom results page gives me a 404 ( /search works, /search/page/2 doesn't). I want to have a url "site.com/search" that will only search and paginate results for custom post type "doctors".

My permalink structure is "post name".

Here I register post_type in functions.php

    $labels = array(
        'name' => __( 'Doctors' ),
        'singular_name' => __( 'Doctor' ),
        'all_items' => __('All Doctors'),
        'add_new' => _x('Create Doctor Profile', 'Doctor'),
        'add_new_item' => __('Create Doctor Profile'),
        'edit_item' => __('Edit Doctor Profile'),
        'new_item' => __('Create Doctor Profile'),
        'view_item' => __('View Doctor Profile'),
        'search_items' => __('Search Doctors'),
        'not_found' =>  __('No Doctors'),
        'not_found_in_trash' => __('No Doctors found in deleted bin.'),
        'parent_item_colon' => ''

    $args = array (
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'public' => true,
        'publicly_queryable' => true,
        'menu_icon' => 'dashicons-businessman',
        'rewrite' => array('slug'=>false,'with_front' => false),
        'query_var' => true,
        'menu_position' => 2,
        'supports'  => array( 'revisions' ,'title', 'thumbnail')


    register_post_type( 'doctor' , $args );

Then I created a template file results.php with the following:


 * Template Name: Results Page

 * Description: Doctor results page.




        <div id="primary_content" class="content-area">

            <div id="content" class="fullwidth" role="main">

                $paged = 1;
                if ( get_query_var('paged') ) $paged = get_query_var('paged');
                if ( get_query_var('page') ) $paged = get_query_var('page');

                  $query_args = array(
                    'post_type' => 'doctor',
                    'paged' => $paged
                  // create a new instance of WP_Query
                  $the_query = new WP_Query( $query_args );

                <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
                    <h1><?php echo the_title(); ?></h1>
                    <div class="excerpt">
                      <?php the_excerpt(); ?>
                <?php endwhile; ?>

                <?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
                  <nav class="prev-next-posts">
                    <div class="prev-posts-link">
                      <?php echo get_previous_posts_link( 'Previous'); // display older posts link ?>
                    <div class="next-posts-link">
                      <?php echo get_next_posts_link( 'Next',$the_query->max_num_pages ); // display newer posts link ?>
                <?php } ?>

                <?php else: ?>
                    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
                <?php endif; ?>
            </div><!-- #content .site-content -->

        </div><!-- #primary .content-area -->

<?php get_footer(); ?>

I added a new page in admin panel with results page as the template. I visit "site.com/search/" and I see my 10 results. So far so good, but when I click "next" it goes to "site.com/search/page/2" and gives me a 404 page.

I tried adding this in functions.php but it causes a 404 page even on /search page without pagination.

function custom_posts_per_page( $query ) { 
        $query->set( 'post_type', array('doctor') );
    return $query; 
if ( !is_admin() ) 
add_filter( 'pre_get_posts', 'custom_posts_per_page' ); 

When I switch to default permalinks,

url becomes "/?page_id=614", and pagination works "/?page_id=614&paged=2"

How do I get /search/page/2 to work? In the end ideally it will look like /search/page/2?name=deborah&location=los_angeles

I have over 30 posts in that post_type. Any guidance would be greatly appreciated. Thank you.


Reading this post How do I paginate search results for custom post types?

Am I going about it all wrong then? This person claimed search should be done on the main search template, and using rules in permalink page to rewrite it to the way I want it to look?

| improve this question | | | | |
  • Have you flushed your Permalinks (off then on)? – Aibrean Dec 18 '14 at 14:26
  • Yes. I have. Further investigating showed that the optimal choice is to use main search query and rewrite the url to look friendly. Unless I can get this working, I'll be going that route. I was able to get it to work using main search. Just not this custom one. – Darius Dec 18 '14 at 14:30

Your Answer

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

Browse other questions tagged or ask your own question.