Custom Post Type Pagenation and URL Conflicts

I have a custom post type, lets say ‘work’.
There are some categories under work, ‘taskA’ and ‘taskB’
There are some posts {post1, post2, post3} in category taskA and some posts {post4,post5,post6} in category taskB.

I want to list all posts @ mydomain.com/work
So i created a page with title work.
And listed all posts.

Read More

All posts under ‘work’ should have the url as mydomain.com/work/post1
So while registering the custom post type, i used the rewrite slug as ‘work’.
Everything was working fine until i wanted pagenation to work.
On accessing url, mydomain.com/work/page/1, error 404 is thrown.

On changing the rewrite slug as ‘works’, error 404 is resolved.

What approach should I follow to maintain the desired url structure?

Also I want to list all posts under ‘taskA’ @ mydomain.com/work/taskA
Using rewrite slug as ‘work’ didnot help. On using rewrite slug as ‘work-task’, everything seems working fine.
But the url looks like mydomain.com/work-task/taskA
Any Suggestions for change in approach?

Related posts

Leave a Reply

1 comment

  1. Found a solution!

    Deleted the page work.
    And instead of displaying all posts from custom page template,displayed it from archive.php.
    Pagenation will start working at this step.

    For Taxonomy, i changed the slug to ‘work’, and added following code in functions.php.

    add_filter( 'rewrite_rules_array', 'blog_rewrite_rules' );
    function blog_rewrite_rules( $rewrite_rules )
    {
      $topics = get_terms('work-category', array('hide_empty' => true));
      if(is_wp_error($topics))
        return $rewrite_rules;
    
      $newRules = array();
    
      foreach($topics as $topic){
        $newRules['work/'.$topic->slug.'/page/?([0-9]{1,})/?$'] = 'index.php?work-category='.$topic->slug.'&paged=$matches[1]';
        $newRules['work/'.$topic->slug.'/?$']                   = 'index.php?work-category='.$topic->slug;  
      }
    
      return array_merge($newRules,$rewrite_rules);
    }
    

    Url Structure maintained and pagenation works as well!