WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] View Custom Post Types as a List?

[Resolved] View Custom Post Types as a List?

  • First, not sure if this should be in How-To or Themes so apologies if this is in the wrong spot. It seems more how-to to me. My question is how to display a list of entries for a given custom post type.

    After reading this excellent tutorial about custom post types:

    Custom post types in WordPress

    and in the “Viewing a custom post type section,” it appears there are two choices to view a page with custom post type data. One is to use single.php for both “normal” post types (e.g. blog) and custom post types or use single-post_type_name.php to display the custom post type only.

    When I use single.php for normal and custom post types, and I call up the URL for a page that is from a custom post type, I see the individual post. A good thing. When I use the dedicated single-post_type_name.php template file and use the same URL for the entry, I only see the latest entry for my custom post type, not the post that uses the URL. Like this:

    someurl.com/fred-flintstone

    shows a post about Barney Rubble because Barney’s post is more recent. When using single.php, however, I see Fred’s data only.

    My question: how can I display a page that lists all posts of a given custom post type?

    Thanks in advance for any help. Let me know if you need more detail.

Viewing 15 replies - 1 through 15 (of 15 total)
  • http://codex.wordpress.org/Pages#A_Page_of_Posts_for_a_Custom_Post_Type

    Note: Version 3.1 will have some other capabilities for custom post types.

    Thanks, Mike, a follow up question: since this would be for an archive list page, what would the template file name be that WordPress would recognize where I would put this code? Would it be archives-custom_post_type_name.php or something else?

    You would make that as a Page Template.

    See Page Template

    Sorry I wasn’t clear: I didn’t think of a page template because I don’t believe they paginate. My goal is to get a list of entries for my custom post type that includes previous/next links so people can browse n number of entries per list page.

    Are you saying that creating a template, assigning it to a page, and then putting the code into the template will paginate automatically?

    Just want to clarify. Thanks.

    That is what I’m saying. The example I cited does paginate when used with the Twenty Ten theme but here’s another example that might fit your need:
    http://wordpress.org/support/topic/taxonomy-pagination-1?replies=20#post-1750529

    Thanks again for your patience, Mike. Now I get a fatal error:

    “Fatal error: Call to a member function get() on a non-object in /home/stcphoenix/www/www/wp-test/wp-includes/query.php on line 27”

    Here’s the code in my template:

    <?php
    $type = 'stcphx_members';
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args=array(
      'post_type' => $type,
      'post_status' => 'publish',
      'paged' => $paged,
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    );
    $temp = $wp_query;  // assign orginal query to temp variable for later use
    $wp_query = null;
    $wp_query = new WP_Query($args); 
    
    	if (have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    Any ideas? Appreciate your help/ideas.

    Hopefully you are putting that code in your theme’s proper template. If necessary paste ALL your code in that template into wordpress.pastebin.com and report the link back here and maybe someone can spot the problem.

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

    Yes, the template page is in my theme folder with the name of the theme commented out, then a call to header(), then the code above, then calls to the sidebar() and footer() functions. Plain vanilla.

    Here’s the pastebin code: http://wordpress.pastebin.com/k6Q9rtba

    Of course I get “Sorry, no posts matched your criteria.” but your code does execute properly in a Page Template for me.

    [edit]

    Changing the post_type to book (my custom post type) and it does display properly for me.

    Naturally it works for you. 😎 I get the same fatal error. Does WP have a debug mode like Expression Engine where it outputs every query run?

    Have you looked at “Simple Custom Post Type Archives” plugin? It generates support for custom post type archives, including pagination.

    Actually I’m trying to avoid plugins for the same reason I hand code and drive a manual transmission. In theory, the less complexity and the less automation the fewer problems. I also want to learn what the plugins hide from me. I’m a Luddite.

    Thanks for your help. Hopefully I can figure it out from here.

    Turns out to be a plugin conflict. I had started with a few plugins but left one activated by accident: Magic Fields. I shut off plugins and then activated them one by one. Also, with debug mode on, I still get what looks like a fatal error but the page still displays content fine. Here’s the error:

    Notice: Trying to get property of non-object in /home/sitename/www/wp-includes/query.php on line 562

    Thanks for your help guys. Had no idea WP is so verbose and generates so many errors. I’ll have to clean up the ones I can.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘[Resolved] View Custom Post Types as a List?’ is closed to new replies.