WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A bit of a noob PHP question coming up.

I am trying to create a generic archive style view for my custom post types. Ideally I want to create one generic template that can be used for all post-types.

Currently I use this as my have_posts query to bring in all posts of 'custom-post-type'

   'post_type' => 'custom-post-type.',
   'post_status' => 'publish',
   'posts_per_page' => -1,
   'caller_get_posts'=> 1
 $my_query = null;
 $my_query = new WP_Query($args);
 if( $my_query->have_posts() ) {
 while ($my_query->have_posts()) : $my_query->the_post(); 

What I want to do is swap out custom-post-type with the slug of the page so that whenever this template is assigned to a page, it will look for a custom-post-type of that name & list them.

Alternatively if you know of a better way of doing it - great!

Cheers, George

share|improve this question

Try this:

$page = $wp_query->get_queried_object();
$slug = $page->post_name;

PS I think next WP release will get proper custom post archives or something around that...

share|improve this answer
Sorry, can't get this to work. – George Wiscombe Oct 21 '10 at 9:06
Details? If you use this inside function don't forget global $wp_query;. – Rarst Oct 21 '10 at 9:31
up vote 1 down vote accepted

As @Rarst mentioned in the previous comment this is going to be patched in WP3.1

The template hierarchy is "archive-customposttypename.php".

WordPress 3.1 template hierarchy

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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