Let’s say I create a custom post type called ‘new projects’, with capability type = page and slug = ‘projects’.
Let’s then say that I create a new page of type ‘new projects’ and name it ‘my new project’. By default the URL would be represented as:
mysite.com/projects/my-new-project/
However, if I want the page my-new-project to be the default page (like an index) for that custom post type, is that possible? In other words, I would like mysite.com/projects/ to load the my-new-project page.
If this is possible, could I then add more pages of type ‘new projects’ and still define one of those pages as the ‘default’ page?
Thanks so much,
Tathiana
You can kind of fake it before the main query is run with the
pre_get_posts
action:Make sure to change
new_projects
to the actual name you’ve registered the post type under. It will still behave in every way as if it’s the archive page, but only that single post will be queried. You could save the slug in an option so you could change it without having to edit the code.Not sure I’m following entirely and/or if this is the best way around it… but you could try creating a page template, (a “normal” page) and within the code, you can modify the query to only pull in that particular CPT.
Create new page template file,
page-new-projects.php
.Ensure you properly name the template accordingly (this is an example):
Alter the loop of the
page-new-projects.php
to only include'post_type'=> 'new-projects'
(NOTE: custom post types cannot contain spaces, which is why I added the-
)Create a new page within WordPress, titled “projects” and ensure you select the
New Projects
page template (or whatever name you pass through in Step 2).Admittedly, you lose me when you ask if you can add more pages of type ‘new projects’ and still define one of them as the default page, but perhaps the above page-template may shed some light on what you ultimately want to do.
As an example, I envision the loop to run in the following fashion:
Again, there may be a better way around this…
References:
1) http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates
2) http://codex.wordpress.org/Class_Reference/WP_Query
You can definitely do this within WP- it’s not altogether different from having a blog index page that displays the single most-recent post, except in this situation you might want what’s displayed on your “new projects” index page to be a specific post ID that doesn’t change until you tell it to change rather than to change when a new post is published. There’s a couple of ways you could do that, but you’d probably have to tag the “featured” post (i.e. that post you want to show up on your /projects/ page) manually somehow via a special/unique category, tag, or custom field. It’s not really any different for custom post types than it would be for regular blog posts- you’re just using a custom page template that displays specific post type(s). You could also hardcode the post ID of the post you want to feature into your “new projects” index template (i.e. the template that outputs the URL /projects/) and then insert a new post ID when you want to change the featured post. It’s up to you how you constrain what gets featured, and how specific you want it to be (e.g. maybe your featured post displayed at /projects/ is the most recently-added project to your “new projects” custom post type, and then it would function exactly like displaying the single most-recent blog post).
If you don’t want to create an index template for your custom post type, you could alter your .htaccess to rewrite the URL for /projects/ to point to /projects/my-new-project/ (you’d have to manually change this every time you want to point to a different post, though). Depending on your server setup your mileage may vary with this, but as a start: