WordPress.org

Forums

[resolved] Custom Post Type Archive Page Issues (19 posts)

  1. fredhead
    Member
    Posted 3 years ago #

    I've got everything working with three custom content types except the archive list pages and category list pages. I don't want to use plugins (tried them, too much complexity, not enough control, insufficient results, conflicts with other plugins). If people can point me to solutions and ideas, I'd be grateful.

    Using one content type as an example, here is my register function:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    I've created an FAQ template and assigned it to a page called FAQs (so the URL is /faqs, as in the rewrite value above, in the register function):

    [Code moderated as per the Forum Rules. Please use the pastebin]

    First problem: when I call up mysite.com/faqs I get an initial page load of 2 items with an "Older Posts" link. When I click the Older Posts link, however, I get a 404 for /faqs/page/2/ URL.

    Second problem: on the mysite.com/faqs page, when I click a category link included in an entry, I get the default Hello World post, not my custom type FAQs entries. I'm using the default WordPress category taxonomy, not custom taxonomies (not needed, in this case).

    When I created an archive-faq.php page, I still got the 404. So I switched to the older method of using and assigning a template.

    Any ideas how to get the /page/2/ URL working with my template, or how to get archive-faq.php to be recognized by WordPress, would be appreciated. Same for getting the categories to display (which are not custom taxonomies).

    Thank you!

  2. keesiemeijer
    moderator
    Posted 3 years ago #

    Put your code in the pastebin and submit it and post the link to it here.
    some further reading: http://wordpress.org/tags/custom-post-type-pagination

  3. fredhead
    Member
    Posted 3 years ago #

    Exactly what code do you need to see? My functions.php is nearly a 1,000 lines long and much of it spurious to this situation. Are there specific custom post type functions you need to see? That's why I posted the register function (so you can see the rewrite value) and the template.

    I'll read the pagination posts you reference. Thank you for that.

  4. CSD_Images
    Member
    Posted 3 years ago #

    I had this problem I got round it two ways. I created a CPT archive page which is the format of archive-CPT name

    change CPT to your post type and then set the loop query to post_type=CPT name.

    So for portfolio you'd have

    archive-portfolio.php

    and your query as:

    query_posts( 'post_type=portfolio')

    Also make sure you refresh your permalinks, just saving is enough. As you need to ensure WP is aware of the CPTs.

  5. fredhead
    Member
    Posted 3 years ago #

    Sorry, keesie, see now that my code was whacked in my original post. Here's the Pastebin URL:

    http://pastebin.com/fSt99VQP

    @CSD_Images, I think that I've tried your approach but will try again. Not finding anything clicking down keesie's tag list, so far.

    Thanks.

  6. fredhead
    Member
    Posted 3 years ago #

    Part of the mystery solved by reverting permalinks to expose the query string values. The "Older Stories" link goes to a page_id value that is for the page instead of a content type. So the querystring values are:

    ?page_id=30&paged=2

    Any ideas the reason the content type is not passed?

  7. fredhead
    Member
    Posted 3 years ago #

    I have an archive template with the filename including the content type name, with the generic WP calls to header, sidebar, and footer, as well as basic content. No wp_query.

    Question: If I call my new content type URL, I should see the basic content, correct? That's how I would test the right template is being called?

    My problem is that doing this generates a 404 page. I want to understand how WP got to that 404 page. By first not obsessing about the wp_query code but instead trying to figure out how to get WP to call the correct template.

    Any ideas how to determine the path WP takes to get to the 404 page?

    The pastebin code is still valid, above. The register_post_type name is 'faq', the URL is /faq, and the archive file is archive-faq.php. None of that appears to work: I get a 404 page all the time.

    Appreciate any help debugging this problem.

    Update: With permalinks "off" to show the querystring, I set the URL to ?post_type=faq then added "xyz" to my archive-faq.php, archive.php, and index.php files, one by one, to match the template hierarchy WP follows. For some reason, my WP install ignores archive-faq.php and archive.php and only uses the index.php template. That's the mystery I'm trying to solve, for now: how to get WP to use the archive-faq.php template.

  8. keesiemeijer
    moderator
    Posted 3 years ago #

    try resaving your permalink structure under Settings > Permalinks

    If you also have a Page called "faq" this could also cause troubles.

  9. fredhead
    Member
    Posted 3 years ago #

    @keesie I've reset permalinks all the time, every test. I also don't have an FAQ page (I should be so lucky!).

  10. keesiemeijer
    moderator
    Posted 3 years ago #

    is your archive template called: archive-faq.php? I will try your code on my testserver

  11. fredhead
    Member
    Posted 3 years ago #

    Yes. The -typename.php is the archive file.

    The problem appears to be changing the slug from the value used to register the content type. Specifically, if I set the rewrite value in $args to 'faqs' while the content type is registered as 'faq' these problems happen. If I set rewrite to true, WP happily displays the index.php template (still ignoring the archive-faq.php and archive.php templates).

    Ideally I want to have the slug and content type name different so that my content types will be custom to the install, not likely to be stepped on by some future WP upgrade or plugin that offers a content type called 'faq' or 'faqs'.

    My goal is to come up with an instance of creating a content type that works to add/edit/delete content, display an archive page, and display a detail/single page. Displaying the archive page eludes me.

    Appreciate your help.

  12. keesiemeijer
    moderator
    Posted 3 years ago #

    If I register with this it works:

    function faqs_register() {
    
    	$labels = array(
    		'name' => _x('FAQs', 'post type general name'),
    		'singular_name' => _x('FAQ Item', 'post type singular name'),
    		'add_new' => _x('Add New', 'FAQ item'),
    		'add_new_item' => __('Add New FAQ'),
    		'edit_item' => __('Edit FAQ'),
    		'new_item' => __('New FAQ'),
    		'view_item' => __('View FAQ'),
    		'search_items' => __('Search FAQ'),
    		'not_found' =>  __('Nothing found'),
    		'not_found_in_trash' => __('Nothing found in Trash'),
    		'parent_item_colon' => ''
    	);
    
    	$args = array(
    		'labels' => $labels,
    	            		'public' => true,
    	            		'publicly_queryable' => true,
    	            		'show_ui' => true,
    	            		'show_in_menu' => true,
    	            		'_builtin' => false,
    	            		'query_var' => true,
    	            		'rewrite' => array('slug' => 'faqs', 'with_front' => false),
    	            		'menu_icon' => get_stylesheet_directory_uri() . '/icons/faqs.png',
    	            		'capability_type' => 'post',
    	            		'has_archive' => true,
    	            		'hierarchical' => false,
    	            		'menu_position' => 4,
    	            		'supports' => array('title','editor','thumbnail'),
    	            		);
    register_post_type( 'faq' , $args );
     }
  13. fredhead
    Member
    Posted 3 years ago #

    Interesting. It works with permalinks on but not with the default permalinks. Even better, the URL /faqs now calls the archive-faq.php template. So thank you!

    For the record, the rewrite value was the only thing you changed in $args?

  14. keesiemeijer
    moderator
    Posted 3 years ago #

    What doesn't work? the pagination?

    For the record, the rewrite value was the only thing you changed in $args?

    I just took the $args of an old custom post type I had and replaced your $args with it.
    also: 'has_archive' => true,

  15. fredhead
    Member
    Posted 3 years ago #

    ... and I notice _builtin is added to $args.

    What doesn't work is the underlying problem of pulling the correct template. Default permalinks uses index.php in all cases if I use the content type name registered in functions.php. When I switch to a defined permalink, the /faqs URL pulls from archive-faq.php.

    Now that /faqs pulls from the right template, I can add content and see if the /page/2/ pagination works.

    Let you know how it goes.

  16. keesiemeijer
    moderator
    Posted 3 years ago #

    are you using an url like this "http://yoursite.com/?post_type=faq" when viewing the faq archive with default permalinks

  17. fredhead
    Member
    Posted 3 years ago #

    Exactly. My hunch is that the default permalinks needs the post_type to be the same as the name registered for the content type. When you use non-default permalinks, that's when you can use rewrite.

    BTW pagination didn't work straight off so I'll work on that today and come back if I absolutely can't figure it out. I appreciate your help!

  18. keesiemeijer
    moderator
    Posted 3 years ago #

    default permalinks needs the post_type to be the same as the name registered for the content type

    Yes, it doesn't use the rewrite slug when the default structure is set.

  19. fredhead
    Member
    Posted 3 years ago #

    Hi,

    If you've not seen, I've posted my pagination question as a separate topic here:

    http://wordpress.org/support/topic/problems-with-custom-content-type-pagination

    I've wasted most of today working through the topic link you posted earlier, trying a vast array of solutions, to no avail. Any ideas would be appreciated if you have time/interest. Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic