Support » Fixing WordPress » Pagination link of custom post type goes to 404 error page

  • Hello,

    I created a custom post type (“works”) and everything works well, but pagination links go to a 404 error page. And I also get this PHP error:

    Trying to get property of non-object in (…)/wordpress/wp-includes/post-template.php on the line 29.

    Why that happens?

    Pagination in regular post type (for blog) works correctly.

    Thanks in advance!

    UPDATE:

    It seems that it throws the error because there are no posts, which might as well be the same reason why the pagination link leads to an error page. Is that correct?

Viewing 1 replies (of 1 total)
  • My code in template-works.php:

    get_home_pagination();
    
    $args = array( 'post_type' => 'works', 'posts_per_page' => 10, 'paged' => $paged );
    $wp_query = new WP_Query( $args );	
    
    if ( $wp_query->have_posts() ) :
    
    	while ($wp_query->have_posts()) : $wp_query->the_post();
    		get_template_part( 'loop', 'works' );
    	endwhile;	
    
    else :
    	_e( 'Nothing here.' );
    endif;

    The same code (except for the post-type) is used in the template-blog.php, where pagination works correctly.

    And then in functions.php:

    function get_home_pagination() {
    	global $paged, $wp_query, $wp;
    	$args = wp_parse_args($wp->matched_query);
    	if ( !empty ( $args['paged'] ) && 0 == $paged ) {
    		$wp_query->set('paged', $args['paged']);
    	  	$paged = $args['paged'];
    	}
    }

    Interestingly, if I type for example the URL “blog/page/456” , the blog template loads with the message “Nothing here”. No 404 error page.

    On the other hand, the URL “works/page/2”, which I expect me to return some posts, ends on a 404 error page – why?

Viewing 1 replies (of 1 total)
  • The topic ‘Pagination link of custom post type goes to 404 error page’ is closed to new replies.