I have been able to register a new post type. 'code' and now I would like to display it. I added

add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {
if ( is_home() )
    $query->set( 'post_type', array( 'post', 'page', 'album', 'movie', 'quote', 'code', 'attachment' ) );
    return $query;

but this makes it show with other blog posts. I want a new page that only displays 'code' posts. I created a file single-code.php but I dont think this is correct. How do I create a new page that just shows code posts?

  • If all you want is a page that shows a list (or excerpts) of that post type, then it's already enabled through your rewrites: myblog.com/?post_type=code. Then you can customize the look with archive-code.php. See codex.wordpress.org/Template_Hierarchy – Tom Auger Aug 15 '12 at 15:40
  • Thats not exactly what I want. I would like myblog.com/code to be the url and then on that page i see all those code posts and only the code posts. How do I make that page? – josh green Aug 15 '12 at 15:52
  • Josh, I think Tom has given a solid answer on this one IMO. – Brian Fegter Sep 14 '12 at 3:44

The simplest way is to create Page (maybe call it "All Codes"). Then, you "hijack" that page's PHP template by creating a PHP file in your theme's directory called page-all-codes.php (note how the "all-codes" part corresponds to the slug of the Page you created).

You can then use WP_Query to get a list of your posts. Your arguments should look like this - feel free to add more to customize your list (like how you want them ordered etc)

$args = array(
  'post_type' => 'code',
  'posts_per_page' => -1

How you use these $args is up to you. The easiest (and in this case, probably not so bad) way is query_posts( $args );.


You should be able to just create the template archive-code.php, and that will loop through your 'code' only custom post types.

And then if you created a single-code.php template page, that will be how your custom post types 'code' will be displayed if it is a single page.

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.