Support » Fixing WordPress » URL conflict between custom post type pagination and individual items

  • Resolved fullofwin

    (@fullofwin)


    I have a custom post type set up, called “portfolio-photos”, that has a listing page (using query_posts) under the URL portfolio/photography (it’s a sub-page of portfolio). In my functions.php, I have the following line within the register_post_type function call:

    'rewrite' => array('slug' => 'portfolio/photography'),

    This is basically so that individual items have a URL like portfolio/photography/page-name – this is working. The list page at portfolio/photography is also working. However, the problem is that when I try to go beyond the first page, e.g. portfolio/photography/page/2, it just gives a 404.

    It seems to be a conflict between the paging for portfolio/photography and the rewrite in functions.php that sets individual items up to have that URL. If I change one of them to a different name, both the paging and individual items work. Anybody know how I can fix this issue?

    Note: the portfolio page, which indexes several custom post types (including photography), is working and is functioning with pagination. It seems to be child pages, which should index a single custom post type (e.g., artwork, photography) that struggle with the pagination.

    Here is the code from the template I’m using for the list page, if it’s of any use:

    <?php
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	query_posts(array(
    		'post_type' => array('portfolio-photos'),     // filter by post types
    		'posts_per_page' => 5,
    		'paged' => $paged    // set the current page
    	));
    
    	if (have_posts()): while ( have_posts() ) : the_post();		// loop through the posts - post template below this line
    ?>
    
    	<div class="thumbnail"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_excerpt(); ?></a></div>
    	<h3 class="worktitle"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>, created in <?php the_time('F Y'); ?></h3>
    	<p class="tagged"><?php echo get_the_term_list( $post->ID, 'portfolio-tags', ' Tagged under ', ', ', '.' ); ?></p>
    	<div class="hrmedium"></div>
    
    <?php
    		endwhile;	// end loop
    	else:
    		echo "<p>No posts found.</p>";	// message to show if nothing is found
    	endif;
    
    	if(function_exists('wp_paginate')) { wp_paginate(); }   // add paging
    
    	wp_reset_query();   // reset the query
    ?>
Viewing 10 replies - 1 through 10 (of 10 total)
  • Still haven’t found a solution. Anyone know anything about this issue?

    Hey fullofwin,

    I was having a similar problem with a custom post type. For me it seems that problem was with my permalinks which were confusing the paging. So going to /page/2 made wordpress look for a post named “page”. I found a great, and small, plugin that fixed the issue, you can grab it from here:

    http://barefootdevelopment.blogspot.com/2007/11/fix-for-wordpress-paging-problem.html

    Hopefully that solves it for you, good luck!

    Thanks Ian – I see what the plugin is doing, but didn’t seem to work for this problem. 🙁

    Maybe could modify it a bit…

    I too am having the same problem. Hope someone could help

    Here is modified code from the link I sent before that might work for you guys:

    function remove_page_from_query_string($query_string)
    {
    	if ($query_string['name'] == 'page' && isset($query_string['page'])) {
    		$post_type = $query_string['post_type'];
    		list($delim, $page_index) = split('/', $query_string['page']);
    		$query_string = array();
    		$query_string['pagename'] = $post_type;
    		$query_string['paged'] = $page_index;
    	}
    	return $query_string;
    }
    
    add_filter('request', 'remove_page_from_query_string');

    Dropping by to say that this seems to have been fixed (how, is a mystery!). Upgrading to the latest version solved this for me.

    Scratch that. It’s not fixed. Will try ianhirschfeld’s second suggestion today and report back.

    Got it worked out finally! (I’m working on leliathomas’ site, by the way.)

    I messed around with the function you posted, Ian, and worked out that to fix it I needed to make it set $query_string[‘pagename’] to a part of the URL like “portfolio/photography”, rather than the post type.

    I’ve done it kind of a hacky way by getting the current URI and grabbing the parts I need from it, so I’m sure there’s a more correct way to do it, and hopefully this doesn’t cause issues on any other pages (doesn’t seem to so far), but this is what I came up with which fixed the problem:

    $current_uri = $_SERVER['REQUEST_URI'];
    $GLOBALS['url_segment'] = explode('/', $current_uri);
    
    function remove_page_from_query_string($query_string)
    {
    	if ($query_string['name'] == 'page' && isset($query_string['page'])) {
    		list($delim, $page_index) = split('/', $query_string['page']);
    		$query_string = array();
    		$query_string['paged'] = $page_index;
    		$query_string['pagename'] = $GLOBALS['url_segment'][1] . '/' . $GLOBALS['url_segment'][2];
    	}
    	return $query_string;
    }
    
    add_filter('request', 'remove_page_from_query_string');

    Paging is working now! Thanks for the help!

    THANK YOU, ianhirschfeld! Your solution brought me back from the brink of frustration!

    In case it is of any worth to anyone I edited ianhirschfeld’s code (above) to work with a permlink structure of /%year%/%category%/%post_name%/%post_id%/ :

    function remove_page_from_query_string($query_string)
    {
    	if ($query_string['name'] == 'page' && isset($query_string['year'])) {
    		$post_year = $query_string['year'];
    		$page_index = $query_string['p'];
    		$page_num = $query_string['p'];
    		$cat_name = $query_string['category_name'];
    		list($delim, $page_index) = split('/', $query_string['page']);
    		$query_string = array();
    		$query_string['year'] = $post_year;
    		$query_string['category_name'] = $cat_name;
    		$query_string['paged'] = $page_num;
    	}
    	return $query_string;
    }
    
    add_filter('request', 'remove_page_from_query_string');

    Obviously this is nothing that ianhirschfeld’s example didn’t cover, but something for comparison for PHP-unfamiliar users out there.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘URL conflict between custom post type pagination and individual items’ is closed to new replies.