Title: Query posts inside custom post type
Last modified: August 21, 2016

---

# Query posts inside custom post type

 *  Resolved [scienartic](https://wordpress.org/support/users/scienartic/)
 * (@scienartic)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/query-posts-inside-custom-post-type/)
 * Hello friends,
 * this is my first post here and im sorry that i have to start it with a question(
   i’m very new to wordpress).
 * I’ve managed to create a custom post type “Product” with many custom fields. 
   I also have written my own template and made a page where i can list all my products.
   When a user clicks on a product, he gets to the single product page where all
   custom fields are shown. That’s working great! Now on the same site (single “
   product”), i want to show the user all articles which i’ve tagged with the products
   name.
    I’ve also managed to do that but here comes the issue: How to enable paging
   on the single product? When i add “/page/2/” at the end of the URL of my single“
   product” (so it looks like localhost/product/testproduct/page/2/) it jumps back
   immediately to the previous url (without the paging).
 * Is there a way to do this? Any info would be helpful!
    Thank you very much! -
   ScienArtic

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [lidya1859](https://wordpress.org/support/users/lidya1859/)
 * (@lidya1859)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/query-posts-inside-custom-post-type/#post-4835016)
 * have you set ‘paged’ => $paged at query_posts arguments?
 * source: [http://codex.wordpress.org/Pagination](http://codex.wordpress.org/Pagination)
 *  [Piyush](https://wordpress.org/support/users/piyushpesw/)
 * (@piyushpesw)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/query-posts-inside-custom-post-type/#post-4835040)
 * hi you can use this code
    please put this code instead of this code
 *     ```
       <?php if ( have_posts() ) : ?>
   
       <!-- Add the pagination functions here. -->
   
       <!-- Start of the main loop. -->
       <?php while ( have_posts() ) : the_post();  ?>
   
       <!-- the rest of your theme's main loop -->
   
       <?php endwhile; ?>
       <!-- End of the main loop -->
   
       <!-- Add the pagination functions here. -->
   
       <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
       <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
   
       <?php else : ?>
       <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
       <?php endif; ?>
       ```
   
 * Replace with
 *     ```
       <div class="pagin"><span class="paginat">
       			<?php
       			$big = 999999999;
       			echo paginate_links( array(
       				'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
       				'format' => '?paged=%#%',
       				'current' => max( 1, get_query_var('paged') ),
       				'total' => $wp_query->max_num_pages,
       				'show_all'     => False,
       				'end_size'     => 1,
       				'mid_size'     => 5,
       				'prev_next'    => True,
       				'prev_text'    => __('PREV'),
       				'next_text'    => __('NEXT'),
       				'type'         => 'plain',
       				'add_args'     => False,
       				'add_fragment' => ''
       				)  );
       			?>
       			</span></div>
       ```
   
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/query-posts-inside-custom-post-type/#post-4835051)
 * For single post pagination (with the <!–nextpage–> quicktag) urls look similar
   to this (without “/page”):
    /ocalhost/product/testproduct/2/
 * [https://codex.wordpress.org/Function_Reference/wp_link_pages](https://codex.wordpress.org/Function_Reference/wp_link_pages)
 * Or use previous_post_link() and next_post_link() to navigate to the next and 
   previous post.
    [https://codex.wordpress.org/Function_Reference/previous_post_link](https://codex.wordpress.org/Function_Reference/previous_post_link)
   [https://codex.wordpress.org/Function_Reference/next_post_link](https://codex.wordpress.org/Function_Reference/next_post_link)
 *  Thread Starter [scienartic](https://wordpress.org/support/users/scienartic/)
 * (@scienartic)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/query-posts-inside-custom-post-type/#post-4835102)
 * Hello!
    Thank you very much for all your answers. Unfortunately i was not yet
   able to solve the issue.
 * [@lidya1859](https://wordpress.org/support/users/lidya1859/): yes, i use the ‘
   paged’ => $paged in my WP_Query query.
 * [@piyush](https://wordpress.org/support/users/piyush/): Thank you for your code.
   When i replace the required parts, i see a nice counter from 1 to 5 pages with
   a “next” link, but when i click on any links, nothing happens. The page just 
   reloads. I guess this is because the links are adding the /page/[pagenumber] 
   to the url which is not working
 * [@keesiemeijer](https://wordpress.org/support/users/keesiemeijer/): When i add
   the page number to the ULR without /page/, then it is leaving the URL with the
   page number but this does not effect the WP_Query.
 * Edit: I was able to create a pagination! I’ve modified Piyush code so it looks
   like this:
 *     ```
       echo paginate_links( array(
       				'base' => $permalink.'/%#%',
       				'format' => '?page=%#%',
       				'current' => max( 1, get_query_var('page') ),
       				'total' => $wp_query2->max_num_pages,
       				'show_all'     => False,
       				'end_size'     => 1,
       				'mid_size'     => 5,
       				'prev_next'    => True,
       				'prev_text'    => __('PREV'),
       				'next_text'    => __('NEXT'),
       				'type'         => 'plain',
       				'add_args'     => FALSE,
       				'add_fragment' => ''
       				)  );
       ```
   
 * (The $permalink variable is the permalink of the single product)
    And in my WP_Query
   i use ‘paged’ => get_query_var(‘page’)
 * Thank you very very much for your support!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Query posts inside custom post type’ is closed to new replies.

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [list](https://wordpress.org/support/topic-tag/list/)
 * [page](https://wordpress.org/support/topic-tag/page/)
 * [post](https://wordpress.org/support/topic-tag/post/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 4 participants
 * Last reply from: [scienartic](https://wordpress.org/support/users/scienartic/)
 * Last activity: [12 years, 1 month ago](https://wordpress.org/support/topic/query-posts-inside-custom-post-type/#post-4835102)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
