Custom Post Type pagination when CPT ‘rewrite’ rule and a page have the same slug

I would like to achive the following link structure on my site:

  1. /properties/ -> a properties page with properties CPT, page 1.
  2. /properties/page/2 -> a properties page with properties CPT, page 2.
  3. /properties/property-name -> a single properties page with a property details.

I have a Custom Post Type (CPT) called property. It’s registered as following with the rewrite rule specified as 'rewrite' => array('slug' => 'properties', 'with_front' => false):

Read More
function property_post_type_init() {
  $labels = array(
    'name' => _x('Properties', 'post type general name'),
    'singular_name' => _x('Property', 'post type singular name'),
    'add_new' => _x('Add New', 'property')
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'rewrite' => array('slug' => 'properties', 'with_front' => false),
    'query_var' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'show_in_nav_menus' => false,
    'menu_position' => 5,
    'supports' => array(
      'title',
      'editor',
      'revisions'
    )
  );
  register_post_type('property',$args);

I also have a page with the slug properties and the permalink structure is set as /%category%/%postname%/

On the Properties page I construct a new WP_Query object with the following arguments:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => 'property',
    'orderby' => 'date',
    'order' => 'DESC',
    'paged' => $paged
);
return $args;

I use a custom pagination code (in functions.php) which generates pagination correctly with the correct urls:

<my-site>/properties/
<my-site>/properties/page/2/
<my-site>/properties/page/3/

However, when I click on any page in the pagination I get redirected to 404 page. I’ve expected the query params and they are the following:

[page] => 2
[property] => page
[post_type] => property
[name] => page

Which is not correct! I don’t understand where these parameters are coming from:

[page] => 2
[property] => page
[name] => page

I’m sure it’s something to do with the rewrite rule when registering a CPT.

How can I achive the following set up to work correctly:

  1. A properties page that displays all properties custom post types posts with pagination.
  2. A properties custom post type with 'rewrite' => array('slug' => 'properties', 'with_front' => false) rule.
  3. Permalink of /%category%/%postname%/ structure

So that I have the following link structure:

  1. /properties/ -> a properties page with properties CPT, page 1.
  2. /properties/page/2 -> a properties page with properties CPT, page 2.
  3. /properties/property-name -> a single properties page with a property details.

Hope that makes sense.

Thanks,
Dasha

Related posts

Leave a Reply

1 comment

  1. Try changing the page’s slug to something else and changing your post type registration to this:

      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'has_archive' => true,
        'show_ui' => true,
        'rewrite' => array('slug' => 'properties', 'with_front' => false),
        'query_var' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'show_in_nav_menus' => false,
        'menu_position' => 5,
        'supports' => array(
          'title',
          'editor',
          'revisions'
        )
      );
    

    The operative new argument is 'has_archive'. This is new in 3.1, and gives you a /properties/, /properties/page/2, etc. structure by default. Then flush your rewrite rules and see if that fixes it. You may also need to comment out any code you’ve written playing with the query or rewrite rules