logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$20
Paged Custom Post Type Archive

Hello,

I have a custom post type archive that needs to show only 1 post with a next_posts_link and previous_posts_link at the bottom.

I've got the link showing up but when it gets clicked it doesn't show the previous post but a 404 page instead.

My permalink is set as: /%category%/%postname%

Here's the link: http://jacksonsafricansafaris.com/past-trips

Here's the functions creating the custom post type:
	// REGISTER CUSTOM POST TYPE FOR PAST TRIPS
function past_trips_post_type() {
register_post_type('past-trips',array(
'labels' => array(
'name' => 'Past Trips',
'singular_name' => 'Past Trip',
'menu_name' => 'Past Trips',
'add_new_item' => __('Add Past Trip'),
'edit' => __('Edit'),
'edit_item' => __('Edit Past Trip'),
'new_item' => __('New Past Trip'),
'view' => __('View Past Trip'),
'view_item' => __('View Past Trip'),
'search_items' => __('Search Past Trips'),
'not_found' => __('No Past Trips found'),
'not_found_in_trash' => __('No Past Trips found in Trash'),
),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'supports' => array('title','editor','thumbnail', 'custom-fields', 'comments'),
'menu_position' => 25,
'rewrite' => array(
'slug' => 'past-trips',
'with_front' => false
),
'capability_type' => 'post',
'hierarchical' => false,
'query_var' => true,
'has_archive' => 'past-trips',
));
}
add_action('init', 'past_trips_post_type');


Here's the code for the archive-past-trips.php template:
<?php get_header(); ?>

<section id="main-content" class="grid_16 group">

<div class="posts grid_11 alpha group" id="post-<?php the_ID(); ?>">

<h1><?php post_type_archive_title(); ?></h1>

<?php query_posts('post_type=past-trips&posts_per_page=1&paged=>$paged'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php $date = get_post_meta($post->ID, 'Past Trip Date', true); ?>
<?php $country = get_post_meta($post->ID, 'Country', true); ?>

<div class="entry group">
<header class="post-header group">
<span class="category-title"><?php echo $country; ?></span>
<h2 class="post-title">
<a title="Permalink to <?php the_title(); ?>" href="<?php the_permalink(); ?>">
<?php the_title(); ?></h2>
</a>
<span class="date floatleft"><?php echo $date; ?></span>
<span class="more-link floatright">
<a title="Permalink to <?php the_title(); ?>" href="<?php the_permalink(); ?>">&gt;More Info</a>
</span>
</header><!-- post-header -->

<div class="post-thumb floatleft">
<?php if ( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail(array(140,100)); ?>
<?php } else { ?>
<img alt="no image available" src="<?php bloginfo('template_url');?>/images/tmp-featured-image-140.jpg" width="140" height="100">
<?php } ?>
</div><!-- post-thumb -->

<div class="the-excerpt floatleft group">
<?php the_excerpt(); ?>
</div><!-- the-excerpt -->
</div><!-- entry -->

<?php endwhile; ?>

<nav class="navigation">
<ul>
<li class="next-posts post-nav"><?php next_posts_link('&lt; Older Past Trips') ?></li>
<li class="prev-posts post-nav"><?php previous_posts_link('Newer Past Trips &gt;') ?></li>
</ul>
</nav>

<?php endif; ?>



</div><!-- post-ID -->

<?php get_sidebar(); ?>

</section><!-- main-content -->

<?php get_footer(); ?>

Denis Leblanc | 04/04/11 at 5:12pm Edit


The experts have suggested, on average, a prize of $20 for this question.

(5) Possible Answers Submitted...

Note: Denis Leblanc felt their question was unanswered, so we granted them a refund.

Note: Denis Leblanc requested a refund. They offered this explanation:

"Problem didn't get resolved."

If no one challenges a refund request, then they are automatically granted and proccessed after 48 hours. Admins of this site only review refund requests if someone challenges the request. If you are curious about how we handled previous refund requests, you may read over all refund requests and their challenges.

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    04/04/11
    5:21pm
    Maor Barazany says:

    Try to change this line in your query in the archive-past-trips.php file

    From -


    <?php query_posts('post_type=past-trips&posts_per_page=1&paged=>$paged'); ?>


    to this -


    <?php $args = array ('post_type'=> 'past-trips', 'posts_per_page' => 1, paged =>$paged);
    query_posts($args); ?>


    Also, re-save your permalinks structure.

    Previous versions of this answer: 04/04/11 at 5:18pm | 04/04/11 at 5:21pm

    • 04/04/11 5:30pm

      Denis Leblanc says:

      Maor,

      Replaced the query with yours and re-saved the permalinks and I still get the 404.


      Denis

    • 04/04/11 5:31pm

      Denis Leblanc says:

      Is it possible that there is something missing in my function?


      d.

    • 04/04/11 5:40pm

      Maor Barazany says:

      Try this one, another way for this to work -


      <?php global $paged;
      query_posts('post_type=past-trips&posts_per_page=1&paged='.$paged); ?>

    • 04/04/11 5:45pm

      Maor Barazany says:

      Aslo, try changing in your register_post_type function

      From this


      'query_var' => true,


      to this -

      'query_var' => 'past-trips',

    • 04/04/11 5:56pm

      Denis Leblanc says:

      This is very odd. Still not working. Changed the query and function and still getting the 404.

      d.

    • 04/04/11 5:56pm

      Denis Leblanc says:

      This is very odd. Still not working. Changed the query and function and still getting the 404.

      d.

    • 04/04/11 6:16pm

      Maor Barazany says:

      Try adding this before the query_posts line -


      $paged = get_query_var('paged');
      $paged = ($paged) ? $paged : 1;

    • 04/04/11 7:20pm

      Denis Leblanc says:

      Not working either.

      Here's my updated code:


      <?php get_header(); ?>

      <section id="main-content" class="grid_16 group">

      <div class="posts grid_11 alpha group" id="post-<?php the_ID(); ?>">

      <h1><?php post_type_archive_title(); ?></h1>

      <?php $paged = get_query_var('paged'); ?>
      <?php $paged = ($paged) ? $paged : 1; ?>
      <?php $args = array ('post_type'=> 'past-trips', 'posts_per_page' => 1, paged =>$paged); ?>
      <?php query_posts($args); ?>
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

      <?php $date = get_post_meta($post->ID, 'Past Trip Date', true); ?>
      <?php $country = get_post_meta($post->ID, 'Country', true); ?>

      <div class="entry group">
      <header class="post-header group">
      <span class="category-title"><?php echo $country; ?></span>
      <h2 class="post-title">
      <a title="Permalink to <?php the_title(); ?>" href="<?php the_permalink(); ?>">
      <?php the_title(); ?></h2>
      </a>
      <span class="date floatleft"><?php echo $date; ?></span>
      <span class="more-link floatright">
      <a title="Permalink to <?php the_title(); ?>" href="<?php the_permalink(); ?>">&gt;More Info</a>
      </span>
      </header><!-- post-header -->

      <div class="post-thumb floatleft">
      <?php if ( has_post_thumbnail() ) { ?>
      <?php the_post_thumbnail(array(140,100)); ?>
      <?php } else { ?>
      <img alt="no image available" src="<?php bloginfo('template_url');?>/images/tmp-featured-image-140.jpg" width="140" height="100">
      <?php } ?>
      </div><!-- post-thumb -->

      <div class="the-excerpt floatleft group">
      <?php the_excerpt(); ?>
      </div><!-- the-excerpt -->
      </div><!-- entry -->

      <?php endwhile; ?>

      <nav class="navigation">
      <ul>
      <li class="next-posts post-nav"><?php next_posts_link('&lt; Older Past Trips') ?></li>
      <li class="prev-posts post-nav"><?php previous_posts_link('Newer Past Trips &gt;') ?></li>
      </ul>
      </nav>

      <?php endif; ?>



      </div><!-- post-ID -->

      <?php get_sidebar(); ?>

      </section><!-- main-content -->

      <?php get_footer(); ?>

    • 04/04/11 8:04pm

      Maor Barazany says:

      Sorry, had small mistake, This line -


      <?php $args = array ('post_type'=> 'past-trips', 'posts_per_page' => 1, paged =>$paged); ?>


      should replaced with (I forgot to wrap the first paged with '' ) -


      <?php $args = array ('post_type'=> 'past-trips', 'posts_per_page' => 1, 'paged' =>$paged); ?>

    • 04/04/11 9:39pm

      Denis Leblanc says:

      Maor,

      I changed that line and still not luck. Are you sure this line is correct:

      <?php $paged = ($paged) ? $paged : 1; ?>

    • 04/05/11 4:32am

      Maor Barazany says:

      Yes, this line is correct, although in most cases it should work without it also.

      Also, you could try Denzel's suggestion to add the $query_string.' to the query.

    • 04/05/11 12:52pm

      Maor Barazany says:

      Try to deactivate all of your plugins, maybe you have a plugin that breaks this. This is very weird, since the code I gave you works for me great in many sites.

    • 04/05/11 7:04pm

      Denis Leblanc says:

      Maor,

      Deactivated all the plugins and re-saved the permalinks. Still not working. I'm thinking of abandoning this idea.

      I've been running in to a lot of issues with custom post types with this project. They're still not quite up to speed with posts and pages yet.

      This should be easily controllable as they would be with regular posts.

      Thanks for the help.

    • 04/05/11 7:10pm

      Maor Barazany says:

      Well, I use a lot of Custom Post Types in projects, and they work and function great as expected.
      There might be something in your theme causing this. If you would like to check this, maybe try to use a blank theme or the default twenty-ten theme and add there your CPT declaration.

    • 04/05/11 7:32pm

      Maor Barazany says:

      It also might be an issue with WP3.1 bug in certain environments, try to upgrade to the new WP3.1.1 , it might also solve your issue.

      http://codex.wordpress.org/Version_3.1.1

  • avatar
    Last edited:
    04/04/11
    5:57pm
    Sébastien | French WordpressDesigner says:

    if ( get_query_var('paged') )
    $paged = get_query_var('paged');
    elseif ( get_query_var('page') )
    $paged = get_query_var('page');
    else
    $paged = 1;

    $args=array(
    'post_type' => 'past-trips',
    'posts_per_page' => 1,
    'paged' => $paged
    );

    • 04/04/11 7:04pm

      Denis Leblanc says:

      Thanks Sebastien,

      Not having any luck with that code either. Here's my updated code:

      <?php
      if (get_query_var('paged')) {
      $paged = get_query_var('paged');
      } elseif (get_query_var('page')) {
      $paged = get_query_var('page');
      } else {
      $paged = 1;
      }
      ?>
      <?php $args = array ('post_type'=> 'past-trips', 'posts_per_page' => 1, paged =>$paged); ?>
      <?php query_posts($args); ?>

  • avatar
    Last edited:
    04/05/11
    1:46am
    Denzel Chia says:

    Hi,

    Please try my version for your query_posts.
    I had create a theme that needed custom queries and therefore had come up with the working solution before.

    Please try this, the arguments are already included.


    <?php
    query_posts($query_string.'&post_type=past-trips&posts_per_page=1' . '&paged=' . get_query_var('paged'));
    ?>


    Thanks.
    Denzel

    • 04/05/11 11:50am

      Denis Leblanc says:

      Thanks Denzel,

      That doesn't seem to work either.

  • avatar
    Last edited:
    04/06/11
    1:14am
    M H says:

    This might be due to this problem - having the "Blog pages show at most" setting under reading settings read something different than your query.
    The fix is to have that number in the reading settings less than your desired number in the query( eg. set it to 1).

    There are a few more possible issues that cause the 404, for instance, having your Page share the same name as your custom post type.

    These are outlined and discussed in the following links along with their solutions:
    http://wordpress.org/support/topic/error-404-on-pagination-when-changing-posts_per_page-on-query_posts
    http://wordpress.org/support/topic/pagination-with-custom-post-type-listing?replies=23#post-1637753

    Previous versions of this answer: 04/06/11 at 1:14am

  • avatar
    Last edited:
    04/07/11
    2:24am
    Christianto says:

    Hi Denis Leblanc,

    The reason your custom page post turn to be 404 page is because your custom permalink is set to /%category%/%postname%.

    If you use custom permalink wordpress will use it as query strings. For example, Base on your permalink structure if the post url is http://jacksonsafricansafaris.com/past-trips/mypostname it will take post name 'mypostname' and on category 'past-trips'.

    so link
    http://jacksonsafricansafaris.com/past-trips
    will lead you to past-trips category not past-trips custom post type.

    so you will have to edit (use custom post-type query) on category-past-trips.php not archive-past-trips.php

    pointing to
    http://jacksonsafricansafaris.com/past-trips
    for wordpress it means show category-past-trips.php and show posts under past-trips post type.

    But on the other hand...

    if you point your browser to post under 'past-trips' post type for example:
    http://jacksonsafricansafaris.com/past-trips/my-begin-journey
    it will turn to 404 as well

    Like I said it will search for post named 'my-begin-journey' under 'past-trips' category.

    I try to create custom taxonomy name 'past-trips' but it failed simply because '%category%' format permalink always refer to Categories under 'post' not under your custom post type 'Past Trips', therefore it will turn to 404 since categories and postname mismatch.

    So for showing single post not archive/category the best solution is try to change permalink to default format like http://yourblog.com/?p=123

    And for Category archive the format will be http://yourblog.com/?category_name=past-trips

    Personally I think this is wordpress bug not because your code.

    Previous versions of this answer: 04/07/11 at 2:07am | 04/07/11 at 2:24am

This question has expired.





Current status of this question: Refunded



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.