3

I realise with this question I was overcomplicating a really simple issue.

My question is really this:

How to have a page listing all items of a given custom post type?

Say the custom post type is foobar, I guess I want to be able to have a URL mydomain.com/foobar which will list them all. No categories involved. And with a template file in PHP which I can entirely customise.

Rather than edit the earlier question I have left it there in case the answers there are useful and applicable should someone need a "page without a page". But cybmeta is right, in the comments on that question, all I need in my case is an archive page.

3

Thanks again to cybmeta for his comments in my earlier question.

Of course the solution is to use an archive template. The filename will be archive-foobar.php. The target URL will indeed be mydomain.com/foobar.

This works fine, even though for my purposes "archive" is a misnomer. The end user never sees the word "archive".

Additionally, using the notes here, I needed to remember to:

1) If you want to use a custom archive page for your custom-post-type, add an additional argument to your register_post_type() arguments: 'has_archive' => true...

(This will tell WP to look for an archive template according to the codex template hierarchy using the current slug setting of your custom post type. If you want a custom slug for your archives, replace TRUE with the slug name for example: 'has_archive' => 'products' might be the slug you want to use with a 'product' post_type.)

2) Visit the Settings -> Permalinks page to flush your current rewrite rules and rebuild with the new rules for your custom archive.

1
  • I'm glad to help. I would add the use of pre_get_posts action hook if you need to modify the list of post in the acrhive page instead of using a custom query or custom loop in the template file. – cybmeta Jan 5 '15 at 11:08
1

You can use :

<?php $args = array('post_type'=>'your-post-type','post_status'=>'publish','posts_per_page'=>9999)
      $query = new Query($args);

if($query->have_posts){
    while($query->have_posts())
    {
        $query->the_post();
        //Do whatever display you want here
    }
}
?>
0

You can add the following code to your function.php, I Never test this but I believe it should solve the problem for you. One thing you needed to do while making use of the code is to change the custom-post-type1... to the name of your own CPT.

function add_my_CPT( $query ) {
  if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array('post', 'nav_menu_item', 'custom-post-type1', 'custom-post-type2', 'custom-post-type2'));
      return $query;
    }
}
add_filter( 'pre_get_posts', 'add_my_CPT' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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