WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] pagination with custom post type (getting a 404)

[Resolved] pagination with custom post type (getting a 404)

  • I am trying to get pagination working with the wp pagenavi plugin and a custom post type (portfolio page) in wordpress and I am having no luck.

    Here is a stripped down version of my portfolio page:

    <?php get_header(); ?>
    
    <?php
      $type = 'portfolio';
      $args=array(
        'post_type' => $type,
        'post_status' => 'publish',
        'paged' => $paged,
        'posts_per_page' => 1,
        'caller_get_posts'=> 1
      );
      $temp = $wp_query;  // assign original query to temp variable for later use
      $wp_query = null;
      $wp_query = new WP_Query($args);
    ?>
    
    <?php if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post(); ?>
    ...
    <?php endwhile; else : ?>
    ...
    <?php endif; ?>
    
    <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
        $wp_query = null; $wp_query = $temp; ?>
    
    <?php get_footer(); ?>

    I have my permalinks structure set to:

    /%postname%/

    and I have re-saved them.

    When I go to page 2 of my portfolio, I get a 404 page. Any idea why this is happening?

    Thanks for the help.

Viewing 14 replies - 1 through 14 (of 14 total)
  • Moderator keesiemeijer

    @keesiemeijer

    try this:

    <?php
      $type = 'portfolio';
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $args=array(
        'post_type' => $type,
        'post_status' => 'publish',
        'paged' => $paged,
        'posts_per_page' => 1,
        'caller_get_posts'=> 1
      );
      $temp = $wp_query;  // assign original query to temp variable for later use
      $wp_query = null;
      $wp_query = new WP_Query();
      wp_query->query($args);
    ?>

    then put your permalinks to the default structure and back to how the structure was

    Wow, that did the trick. Thanks, I have been searching all over for a solution to this.

    So, it looks like you are just checking to see if there is a paged query var and if not setting it to 1?

    Moderator keesiemeijer

    @keesiemeijer

    yes that was what was needed. Can you make this topic resolved please

    You’re missing a $ on this last line:

    wp_query->query($args);

    Should look like this:

    $wp_query->query($args);

    So the final code should look like this:

    <?php
      $type = 'portfolio';
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $args=array(
        'post_type' => $type,
        'post_status' => 'publish',
        'paged' => $paged,
        'posts_per_page' => 1,
        'caller_get_posts'=> 1
      );
      $temp = $wp_query;  // assign original query to temp variable for later use
      $wp_query = null;
      $wp_query = new WP_Query();
      $wp_query->query($args);
    ?>

    Hey guys,

    I just updated to WP 3.1 and have got WP-PageNavi working with Custom Post Types listed with the following query in a page template:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
    	'post_type' => 'press',
    	'paged' => $paged
    
    	); query_posts($args); ?>

    The problem I’m having now is now the individual links to the posts (single.php) are going to a 404 page. I changed the permalinks back to default and it works fine, but when I change my permalinks to anything by default it busts. Any ideas?! I’ll post back any advances on my part.

    Hi. Code from vectyr worked for me on wp 3.0.5

    I’ve been trying to figure this out for hours and finally vectyr’s solution worked (the resetting permalinks to default and then back to previous structure was the final trick!) Thanks so much. This has been driving me crazy.

    RE @sevdestruct

    I was having the same problem as well.. I used Vectyr’s solution and did the reset of permalinks too (actually had to return them to default and then back to how i wanted them), and it worked.

    So strange that that is a necessary step… Is this some kind of bug?

    Just wanted to leave a note.. After this ‘fix’ the issue reappeared, I can’t explain it.

    Toggling the permalink settings again resulted in a ‘fix’

    What is going on here?

    Hi.

    I use the vectyr code to get pagination with custom post type and the “older” and “newer” links are finally displaying.
    I use this loop on home page for displaying “project” custom post type, but my new problem is that the “older…” link http://mywebsite/?paged=2 links to nothing. It gets me back to the home.
    I think the issue is that this link should be http://mywebsite/?page_id=8&paged=2
    (because my home page is set on page wich ID is 8).
    ( I use default permalinks settings).

    How getting this working properly? (with possibility to change permalinks later)

    Hey guys,
    So I’m trying to get Custom Post Type UI and WP-PageNavi.

    I’m having pagination issue’s though. I keep reading all kinds of different ways to accomplish this without any luck.

    Here’s my code:

    <?php $loop = new WP_Query( array( 'post_type' => 'web', 'posts_per_page' => 6 ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    LOOP STUFF HERE
    
    <?php endwhile; ?>
    <?php wp_reset_postdata(); // reset the query ?>
    
    <?php wp_pagenavi(): ?>

    It all works, except when I try to go to the 2nd page of posts.

    Here’s my url when trying to accomplish all this:
    http://www.website.com/test/portfolio/web/page/2/
    (Is this my prob?)

    Here’s my code the Custom Post Type UI generates for me:

    register_post_type('web', array(	'label' => 'Websites','description' => '','public' => true,'show_ui' => true,'show_in_menu' => true,'capability_type' => 'post','hierarchical' => false,'rewrite' => array('slug' => 'portfolio/web'),'query_var' => true,'menu_position' => 4,'supports' => array('title','editor','excerpt','custom-fields','thumbnail',),'labels' => array (
      'name' => 'Websites',
      'singular_name' => 'Website',
      'menu_name' => 'Websites',
      'add_new' => 'Add Website',
      'add_new_item' => 'Add New Website',
      'edit' => 'Edit',
      'edit_item' => 'Edit Website',
      'new_item' => 'New Website',
      'view' => 'View Website',
      'view_item' => 'View Website',
      'search_items' => 'Search Websites',
      'not_found' => 'No Websites Found',
      'not_found_in_trash' => 'No Websites Found in Trash',
      'parent' => 'Parent Website',
    ),) );

    Thoughts? ANYTHING would help.
    Thanks!

    Just to say I found the fix for my problem here.
    (my problem was “Pagination for Custom Post Types on Front Page”).
    It works great for me.

    so just add this as paged param of your loop :

    if ( get_query_var('paged') ) {
        $paged = get_query_var('paged');
    } else if ( get_query_var('page') ) {
        $paged = get_query_var('page');
    } else {
        $paged = 1;
    }
    query_posts( array( 'post_type' => 'post', 'paged' => $paged ) );

    Hi hoss9009

    You do not seem to use the get_query_var(‘paged’) stuff so try this :

    <?php
    	if ( get_query_var('paged') ) {
    	    $paged = get_query_var('paged');
    	} else if ( get_query_var('page') ) {
    	    $paged = get_query_var('page');
    	} else {
    	    $paged = 1;
    	}
    	$loop = new WP_Query( array( 'post_type' => 'web', 'posts_per_page' => 6, 'paged' => $paged ) ); ?>
    	<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    	LOOP STUFF HERE
    
    	<?php endwhile; ?>
    	<?php wp_reset_postdata(); // reset the query ?>
    
    	<?php wp_pagenavi(): ?

    moved our wordpress directory into launch position on our server, and then the permalinks broke on my portfolio tags and client taxonomies!! i then remembered that i have been here before, and recalled the tip stated by vector here. thanks again. the new site has launched: creable.com

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘[Resolved] pagination with custom post type (getting a 404)’ is closed to new replies.