3

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

5
  • 1
    The normal custom post type templates won't work for you?
    – s_ha_dum
    Commented Feb 1, 2013 at 19:41
  • Even if I use a single.php template, won't the title (or whatever you indicate) of the be displayed in URL? Isn't the template just that, a template for how to display the page?
    – Tathiana
    Commented Feb 1, 2013 at 19:55
  • I'm not sure I follow you. You want every post in your "new-projects" post type to load to the same URL-- "mysite.com/projects/my-new-project/" ???
    – s_ha_dum
    Commented Feb 1, 2013 at 20:00
  • No, not quite. And first of all, thanks for taking the time to answer. If the slug for this cpt = "projects" and I create is page of this type called "project one". The resulting URL would be -- "mysite.com/projects/project-one". I want to know if there is a way that I can define the "project one" page as the default page for this custom post type, so that the URL "mysite.com/projects/" will load the "project one" page. I hope this clarifies.
    – Tathiana
    Commented Feb 1, 2013 at 20:05
  • That might be possible. You can sometimes "overload" pages but I don't know about this case and I don't want to try to answer if I'm not pretty sure.
    – s_ha_dum
    Commented Feb 1, 2013 at 21:02

3 Answers 3

1

You can kind of fake it before the main query is run with the pre_get_posts action:

function wpa84126_single_project_archive( $query ){
    if( ! is_admin()
        && $query->is_main_query()
        && $query->is_post_type_archive( 'new_projects' ) )
            $query->set( 'name', 'my-new-project' );
}
add_action( 'pre_get_posts', 'wpa84126_single_project_archive' );

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.

1
  • Thanks Milo! Let me play a bit with this and I will get back go you.
    – Tathiana
    Commented Feb 2, 2013 at 0:40
0

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.

  1. Create new page template file, page-new-projects.php.

  2. Ensure you properly name the template accordingly (this is an example):

    <?php 
    /*
     Template Name: New Projects
    */
    ?>
    
  3. 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 -)

  4. 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:

//set the arguments to pass into WP_Query
$args = array('post_type'=>'new-projects');  //This is assuming 'new-projects' is the custom post type
$query = new WP_Query( $args );

// The Loop
while ( $query->have_posts() ) : $query->the_post();
    //do whatever you want to do within the loop
endwhile;

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

2
  • Ah - I glossed over the part where you mentioned the cpt slug = 'projects'. This may not work as intended :/ Commented Feb 1, 2013 at 20:20
  • Thanks for your response Christian. Unfortunately, I do not see how this solves my inquiry. Not sure if you saw my comment above, but I tried to clarify the scenario to another person: If the slug for this cpt = "projects" and I create is page of this type called "project one". The resulting URL would be -- "mysite.com/projects/project-one". I want to know if there is a way that I can define the "project one" page as the default page for this custom post type, so that the URL "mysite.com/projects/" will load the "project one" page. I hope this clarifies.
    – Tathiana
    Commented Feb 1, 2013 at 20:32
0

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:

<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^projects/$ projects/my-new-project/ [L]
</ifmodule>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.