URL Rewrite with Custom Post Type

Just downloaded roots and so far so good…

However I have a question regarding custom post types. I created a new post type “Auction” in lib/custom.php by using this code

add_action( 'init', 'create_post_type' );

function create_post_type() {
	register_post_type( 'auction',
		array(
			'labels' => array(
				'name' => __( 'Auctions' ),
				'singular_name' => __( 'Auction' )
			),
			'public' => true,
			'has_archive' => true,
			'slug' => 'auction',
      		  	'with_front' => false
		)
	);
}

add_filter('roots_wrap_base', 'roots_wrap_base_cpts'); // Add our function to the roots_wrap_base filter

function roots_wrap_base_cpts($templates) {
	$cpt = get_post_type(); // Get the current post type
	
	if ($cpt) { 
		array_unshift($templates, 'base-' . $cpt . '.php'); // Shift the template to the front of the array
	}

	return $templates; // Return our modified array with base-$cpt.php at the front of the queue
}

Then I went to Settings > Reading and changed “Posts Page” to a page “News” which I created. I also changed the Permalink Structure to “/news/%postname%/

This resulted in a correct structure for the default wordpress post type where I got ‘domain.com/news/’ as the listing page and ‘domain.com/news/hello-world’ as my single page. However I have a problem with my custom post type as now I need to go to ‘domain.com/news/auction/’ to see all the auction entries and ‘domain.com/news/auction/auction-1’ to access the single auction. I am aiming to get to ‘domain.com/auctions/’ whilst still keeping the custom structure for the default wordpress post type.

I though that setting the custom post type to ‘with_front’ as false would avoid this. Am I missing something that is specific to roots.

Any help would be greatly appreciated.

Check the codex or ask on WordPress answers if you need more help, but the ‘slug’ and ‘has_archive’ settings need to be passed to rewrite as an array:

'rewrite' => array('has_archive' => true, 'slug' => 'auction'),

Declaring them in an array doesn’t make a difference. And I don’t know why I should be directed to the codex or Wordpress answers straight away. I just though that Roots would offer this possibility to rewrite the slug for the default post type without prepending that slug to any custom post type created

It’s perfectly possible to do what you want to do in Roots, but you need to add your custom post type in the way that WordPress expects.

You were rightly directed to the Codex and WordPress answers because the problem is your incorrect WordPress code that has nothing to do with Roots.

If you’re going to copy/paste code for free (including some of mine), then you should at least try to debug it yourself before asking for help. It should have been obvious what the problem was if you visited the relevant codex page.

If changing the arguments to an array didn’t help, then you probably just need to flush your permalinks. A note that is also highlighted in the codex.

I had done all the steps you mentioned. I then proceeded to rebuilding the database from scratch with a redeclaration of the post type using the array parameters for the rewrite and it worked. Seems like once you create a post type with certain parameters, Wordpress has a hard time overwriting them.

Thanks for your help though @Foxaii. Much appreciated and well done on the work on Roots!