Support » Plugin: WP-PageNavi » Once Again: Page not found on second and further pages

  • Resolved ValeSauer

    (@valesauer)


    Once again I’m struggling with wp-pagenavi. As I am slowly going crazy I have to ask you for help.

    I am using custom Post Type “topo” and try to get als “topos” within the category “topo-locations”. The problem is, that I get a “Page not found” error when trying to visit the second page. I think there must be something wrong with my query, but I don’t get it… Seeing the “Page not found”-Page means, WordPress musst have jumped from the category.php (where this code is located) to the index.php

    Works:
    http://walter-hoelzler.sauer-medientechnik.de/topo-locations/page/1/
    Doesn’t work:
    http://walter-hoelzler.sauer-medientechnik.de/topo-locations/page/2/

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. ]

    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    
    	$catid = get_query_var('cat');
    	$args = array(
    		'post_type' => 'topo',
    		'post_status' => 'publish',
    		'cat' => $catid,
    		'paged' => $paged
    	);
    	query_posts( $args );
    	if (have_posts()) :
    	while (have_posts()) : the_post();
    //do something
    	endwhile;
    	else:
    	endif;
    	if(function_exists('wp_pagenavi')) { wp_pagenavi(); }

    http://wordpress.org/extend/plugins/wp-pagenavi/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi, I had the same problem in the past. Read http://codex.wordpress.org/Function_Reference/query_posts and try to change query_posts with WP_Query.

    Thanks, romancretnik, that was not a solution for me.

    I don’t know why, but after trying, editing, and saving the slugs of both the categories and custom post types it worked.
    Now that I went live with the page the pagination fell back to its old behaviour and gives back “not found” errors.

    I went through dozens of wordpress forums and stuff, tried so many codes and “fixes”, but none made it for me…

    Is there a way to debug the query? I allready tried it with SAVEQUERIES true, but I couldn’t find an answer in the queries..

    here, once again, the page which is live know, WITH this silly bug…
    http://www.walter-hoelzler.de/tourenberichte/page/2/
    http://www.walter-hoelzler.de/tourenberichte/

    Thats interesting. I found out, that pagination works, if I create as many ordinary posts as i have custom posts.

    If i have 5 posts per page and
    6 topos (my custom post type)
    5 normal posts
    Pagination does not work

    If i have
    6 topos
    6 normal posts
    i get page 2.

    And so on.. this works with further pages to..anyone an idea?

    Okay this is crazy. I simply was not deep enough in Custom Post Types and especially not in the right Use of wp_query.

    I read these to posts:
    http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts
    http://codex.wordpress.org/Function_Reference/query_posts

    These made me:
    1. completely remove wp_query from my category/archive page, as it is allready there by the global Query
    2. Change all secondary querys (for example links in the footer, similar posts and so on) to $posts = new WP_Query($args)
    3. copy all my args in the pre_get_posts function

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. ]

    function my_post_queries( $query ) {
    	// do not alter the query on wp-admin pages and only alter it if it's the main query
    	if (!is_admin() && $query->is_main_query()){
    
    		// alter the query for the home and category pages
    		if(is_home()){
    			$query->set('posts_per_page', 5);
    		}
    
    		if(is_category(38)){
    			$query->set('post_type', 'bericht');
    			$query->set('cat', 38);
    			$query->set('posts_per_page', 5);
    		}
    		if(is_category(2)){
    			$query->set('post_type', 'topo');
    			$query->set('cat', 2);
    			$query->set('posts_per_page', 5);
    		}		
    
    	}
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    To get Childcategories too, I had to change it to

    function my_post_queries( $query ) {
    	// do not alter the query on wp-admin pages and only alter it if it's the main query
    	if (!is_admin() && $query->is_main_query()){
    
    		// alter the query for the home and category pages
    		if(is_home()){
    			$query->set('posts_per_page', 5);
    		}
    
    		if(is_category()){
    			$query->set('posts_per_page', 5);
    
    			$args = array(
    					'child_of' => 38
    					);
    			$childcats = get_categories( $args );
    			foreach ($childcats as $childcat){
    				if(cat_is_ancestor_of( 38, $childcat->term_id )){
    					$query->set('post_type', 'bericht');
    				}
    			}
    
    			if(is_category(38)){
    				$query->set('post_type', 'bericht');
    			}
    
    			$args = array(
    					'child_of' => 2
    			);
    			$childcats= get_categories( $args );
    			foreach ($childcats as $childcat){
    				if(cat_is_ancestor_of( 2, $childcat->term_id )){
    					$query->set('post_type', 'bericht');
    				}
    			}		
    
    			if(is_category(2)){
    				$query->set('post_type', 'topo');
    			}
    		}
    	}
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    @ ValeSauer

    This was the only solution that worked for me after searching for an hour.

    It is with this specific setup of changing the posts page and then querying a custom post type like this.

    Thank you for posting it.

    Hello, I had the same problem with my conferences blog networkevents.ro
    In blog-networkevents every time I clicked on second, third or … next page I get Error 404 Not found page and the system loaded index.php template (the home page of website).
    After searching a lot through forums and wbesite pages where this problem is presented and everyone give some kind of solutions, I tryed every one but without success. I had an idea: to write in url address box in browser the original url of second page: networkevents.ro/index.php?paged=2&category_name=blog-networkevents and hit it. I receive the right page but the url rewrited was: networkevents.ro/category/blog-networkevents/page/2/ not the one I supposed it is: networkevents.ro/blog-networkevents/page/2/. I stripped through Yoast WordPress SEO the category base ‘category’ from url. (This goal can be achieved by editing .htaccess as this webpage show: here )This solved my problem. I think this solution it is closed to the best solution of this problem for many wordpress websites

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Once Again: Page not found on second and further pages’ is closed to new replies.