I have a custom post type called services which has 23 custom posts, i also have a custom taxonomy for all the categories (5 categories).

I am trying to create a list of all the custom posts of services and show only 5 posts at a time which i have done. I am also trying to have them paginated using wp_paginate, I can get page 1, 2 and 3 to page but for some reason i get a 404 on pages 4 and 5.

this is the query i use to page the list:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $wp_query = new WP_Query(array('post_type' => 'services', 'paged'=>$paged, 'posts_per_page' => 5, 'order' => 'DESC' ));

my custom post type code is:

register_post_type( 'services',
            'labels' => array(
                'name' => __( 'Services' ),
                'singular_name' => __( 'Service' )
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'query_var' => 'services',
            'capability_type' => 'page',
            'has_archive' => 'our-services',
            'rewrite' => array( 'slug' => 'our-services/%service_categories%', 'with_front' => false),
            'taxonomies' => array( 'service_categories' ),
            'supports' => array(

and my custom taxonomy code is:

function init_service_categories() {
            'labels' => array(
                'name' => 'Services Categories',
                'add_new_item' => 'Add New Services Category',
                'new_item_name' => "New Services Category"
            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => true,
            'rewrite' => array( 'slug' => 'our-services' ),
add_action('init', 'init_service_categories', 0);

I also use this bit of code so when i go to a services single page it has the category infront of the service.

add_filter('post_type_link', 'glossary_term_permalink', 10, 4);
function glossary_term_permalink($post_link, $post, $leavename, $sample)
    if ( false !== strpos( $post_link, '%service_categories%' ) ) {
        $glossary_letter = get_the_terms( $post->ID, 'service_categories' );
        $post_link = str_replace( '%service_categories%', array_pop( $glossary_letter )->slug, $post_link );
    return $post_link;

I would really appreciate some help on this as I am so lost and do not understand why it doesn't work.


  • What is permalink structure of pages 4 and 5 ? is it different from pages 1, 2 and 3 ? Aug 28, 2013 at 14:35
  • 1
    what sort of page is this on? why are you creating a new query rather than using the default query on a post type archive page? the core of the issue is that the number of pages is calculated based on an entirely separate query from the custom query you run. the page's original query only has 3 pages of posts. if you're running a new query simply to change the posts per page value, you should be using pre_get_posts, not running a new query in the template.
    – Milo
    Aug 28, 2013 at 14:42
  • See: wordpress.stackexchange.com/a/109234/21376
    – s_ha_dum
    Aug 28, 2013 at 15:00
  • I assume that this is the wp_paginate you mean?
    – s_ha_dum
    Aug 28, 2013 at 15:02
  • the permalink is /our-services/page/2/ for page 2 and for page 4 it is /our-services/page/4/ this is on a archive page. using archive-services.php when i load the page the first time wp_paginate works correctly and shows there are 5 pages of posts as there are 23 posts in total so that would = 5 pages. but i cant view page 4 and 5
    – asaunders
    Aug 28, 2013 at 15:09

1 Answer 1


Don't run custom queries in the template to modify query parameters. The main query runs before the template is loaded, doing this is a waste of resources and introduces pagination issues, as you have discovered.

Remove your custom WP_Query, restore the normal loop, and use pre_get_posts to modify query parameters before the query is run.

function wpa_services_posts_per_page( $query ) {
    if ( !is_admin()
    && $query->is_post_type_archive( 'services' )
    && $query->is_main_query() ) {
        $query->set( 'posts_per_page', 5 );
add_action( 'pre_get_posts', 'wpa_services_posts_per_page' );
  • Thank you. you fixed it. Can you explain a bit more why i was wrong to put the main query in the template? I want to upvote your anwser but i need 15 reputation :( you certainly have fixed the issue. Thank you!!
    – asaunders
    Aug 28, 2013 at 16:11
  • WP decides if a page exists and what template to load based on the results of the main query. That main query uses your default posts per page setting, which I'll guess is set to 10. so according to the results of that query, there is nothing beyond the 3rd page. your template query has no connection to that main query, WP isn't "aware" of it, which is why you need to instead modify the main query before it happens.
    – Milo
    Aug 28, 2013 at 16:58
  • Ahh okay that makes a lot more sense now. I think I will be reading a bit more into this thanks again! Im sorry i cant upvote the answer:(
    – asaunders
    Aug 29, 2013 at 8:05

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.