Support » Themes and Templates » Custom post type tags/categories archive page

  • Hi.

    I’ve been playing with custom post types, and whilst initially I’m impressed with them, I have a few issues.

    I have converted some posts to custom type, and have have given them a category and some tags. This display fine and have hyperlinks. However, when I click the hyperlink, I get the archive page of the tag, but only posts from that tag (or category), not from my custom post type.

    What do I need to do?

Viewing 9 replies - 31 through 39 (of 39 total)
  • $arrImages is empty when the script is activated but I don’t know why, because it’s working fine when the script is turned off. I think it’s something wrong with that link used to get all the post details and something has to be changed to work with the custom posts.

    Solved. Show have added ‘attachment’ to

    $post_type = array('post','cpt');

    cool, you might want to do a var_dump on:

    $post_types = get_post_types();

    As the “fix” is overriding the natural behavior or WordPress, therefore any CPT that is added will not work, nor will any other post types unless you add them back in.

    You can also do:

    $all_post_types = get_post_types();
    $post_type = array( $all_post_types );

    This way your taking all registered post types (default one’s and custom one’s) and adding it back into your filter, then again, this might not be what you want to do.

    76blueberries answer worked for me:

    register_taxonomy_for_object_type('category', 'your_post_type');

    The rest still broke my menu if there were sub-menus or labels as submenu headers.

    Thanks a lot Designophobia and Ryansigg! But I suggest making a minor change to the syntax.

    Replace this line:
    if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {

    With this line:
    if ( is_category() || is_tag() || is_home() && empty( $query->query_vars['suppress_filters'] ) ) {

    Here is the full function to be posted in your functions.php:

    add_filter('pre_get_posts', 'query_post_type');
    function query_post_type($query) {
      if(is_category() || is_tag() || is_home() && empty( $query->query_vars['suppress_filters'] ) ) {
        $post_type = get_query_var('post_type');
    	    $post_type = $post_type;
    	    $post_type = array('post','articles','nav_menu_item');
    	return $query;

    In the array:
    $post_type = array('post','articles','nav_menu_item');

    Replace “articles” with your custom post type name. You can add as many post types as you want in the array.

    This snippet of code, allows you to have all post types show in categories (archive page), home page and tag page.


    I have a problem with adding ‘nav_menu_item’ to the $post_type array, because when I do, every menu item I add to my custom menu shows up as a new post in my “Recent posts” widget on the category page (as a blank item linking to a broken page). When I leave out ‘nav_menu_item’, that problem is gone but then my custom menu won’t show up. What to do?

    This code seems work good. $query->set seems like the way to go.

    Now, How do you display a special archive page for your custom posts? Mine aren’t categorized, and the archive shows the blog archive, as if there wasn’t a request.

    I’d need a way to catch the custom post type in an archive page to show a special header. Something like “if is_custom(‘name’) echo $specialheader”.

    Geez, now i’m a real wordpress geek… welcome, eh 😉

    You could create a page template, modify the query, and use this code to display your special header image on the archive page and single posts:

    <?php if (is_singular('my-post-type') || is_page('name-of-the-page')) { ?>
    //your header image
    <?php } ?>

    Tanks for your answer, but I think I found a better one. WordPress will look for an archive page for custom post types; but you have to tell it it’s there. First, when you initialize the custom type, there’s an has_archive option that needs to be ‘true’. Then, you create an archive page named “archive-“[customtypename].

    Then you put whatever you want in this page, like a special header followed by a call to loop-archive, for example.

    See ya people!

Viewing 9 replies - 31 through 39 (of 39 total)
  • The topic ‘Custom post type tags/categories archive page’ is closed to new replies.