Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I created myfile.php with the following content:

global $paged;
if ( get_query_var( 'paged' ) ) {
    $paged = get_query_var( 'paged' );
} else if ( get_query_var( 'page' ) ) {
    $paged = get_query_var( 'page' );
} else {
    $paged = 1;
}

$args = array(
    'post_type' => array('my_custom_post_type'),
    'paged' => $paged,
    'posts_per_page' => 3,
    'ignore_sticky_posts' => 1
);

$wp_query = new WP_Query( $args );
var_dump($wp_query);
if($wp_query->have_posts()) :
    while($wp_query->have_posts()) : $wp_query->the_post();
        // show post
    endwhile;
endif;

next_posts_link('Next posts');

wp_reset_query();
wp_reset_postdata();

and:

I have no idea what am I missing here but I just tried every single suggestion from troubleshooting guide on this page: https://codex.wordpress.org/Pagination - they suggest that "404 - not found" is one of the common problems when the permalinks are set to "Custom Structure" so WP developers are aware of some bug or issue but they don't say how to fix that.

My server is WAMP for Windows 7 (PHP 5.4.3, Apache 2.4.2, mod_rewrite enabled, custom link structures work great expect pagination when "myfile.php" is used as a page other than "Static Front Page").

My .htaccess when "Custom Permalink Structure" is set to "Post name":

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>

# END WordPress

When I enter http://example.com/mypage/page/2 (doesn't work): SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_name = 'page' AND wp_posts.post_type = 'my_custom_post_type' ORDER BY wp_posts.post_date DESC

When I enter http://example.com/?page_id=1234&paged=2 (works): SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'my_custom_post_type' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 2, 2

share|improve this question
    
Have you seen this? –  toscho Sep 9 '13 at 14:03
    
@toscho Yes, suggested answers contain the code that is almost the same like in my question. –  Paul Sep 9 '13 at 14:06
    
    
@s_ha_dum It looks like what I need! However, I still have no idea how to enable pagination. When I do $qry->set( 'paged', $paged ); in functions.php, entire /mypage/ page stops working. –  Paul Sep 9 '13 at 14:32
    
Furthermore, I think that my main query which loads /mypage/ is alright. I'd like to list posts with pagination in a secondary loop on that page. Does your answer cover that situation too? It looks like it's for modifying the main query only. –  Paul Sep 9 '13 at 14:40

1 Answer 1

After hours of struggling I found the cause.

The post type that I registered using register_post_type( 'my_custom_post_type', array( ... ) ); had 'rewrite' => array( 'slug' => 'my_custom_post_type' ). That was taking priority over pagination and that's why "404 - not found" was happening.

If anyone has better explanation of what takes priority and when, please edit my answer. This issue is resolved after editing rewrite to something else.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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