Support » Plugins and Hacks » [Resolved] Show custom posts in category archives and in own archives

[Resolved] Show custom posts in category archives and in own archives

  • nemoprofeta


    I am able to easily access the archive page for my custom posts. However I wanted to show custom posts inside “normal” category archives. So I used this code:

    function my_get_posts( $query ) {
    	if ( is_feed()||is_archive()&&false == $query->query_vars['suppress_filters'])
    		$query->set( 'post_type', array( 'post', 'progetto' ) );
    	return $query;
    add_filter( 'pre_get_posts', 'my_get_posts' );

    It works, but it makes the custom post archives empty.
    What’s the best way to have both?


Viewing 15 replies - 1 through 15 (of 16 total)
  • Plugin Author Michael Beckwith


    I’d consider a brand new wp_query call instead of altering the original query.

    Thanks, what do you mean?

    Plugin Author Michael Beckwith


    Maybe I was a little bit confused. Were you trying to show 2 separate loops inside the 1 template? or trying to pull the two post types into 1 query.

    Mmm, I don’t remember, 6 months are too much.

    Plugin Author Michael Beckwith


    I’ll assume resolved and just mark as such then.

    I *think* I have the same issue! I’ve selected appropriate blog categories for my ‘articles’ (custom post type), but they do not show up in the regular blog category archives, even though I have ‘Has Achives’ set to True.

    In fact, they don’t even show up anywhere unless I link them directly.

    I need for them to show up in the regular blog category archives, along with the regular blog posts in the same category!

    HELP! Thanks!!!

    Plugin Author Michael Beckwith


    msbree, I do believe you will have to add the custom post type to the query parameters for the category archive.

    Try out this snippet from Chris Coyier and see if it helps.

    function myprefix_add_custom_types( $query ) {
      if( is_category() && empty( $query->query_vars['suppress_filters'] ) ) {
        $query->set( 'post_type', array(
         'post', 'your-custom-post-type-here'
    	  return $query;
    add_filter( 'pre_get_posts', 'myprefix_add_custom_types' );

    Make Archives.php Include Custom Post Types

    Michael…you are awesome with your prompt reply! Thank you!!!

    I will try the one from Chris Coyier first and see how that goes!

    I did find another thread here with a different code that might do the same thing, but it’s two years old.


    I’ll post my update here, so others can use this information!
    thanks again!!!

    Plugin Author Michael Beckwith


    I’d go with Coyier’s code first, as I know it’s a current hook that’s available.

    Well I tried it Michael, and the code did bring in my Custom Post Type into the regular blog categories; unfortunately, it brought the entire post into the Category Archive and not just the excerpt! So code removed! 🙁

    Plugin Author Michael Beckwith


    I’d check on your theme template files for that, as it’s not something that the plugin would be in control of. The plugin doesn’t do anything regarding display of the post types/taxonomies, it just makes them available to be used. So I think you were a step closer, you just need to amend some other parts elsewhere in the site.

    I tried it again Michael, and included an excerpt in a ‘test’ article…no dice! In fact, my main nav bar disappeared too, so I removed the code.

    No worries…I will either redo all of these 133 articles as regular posts (arrgghhhh) or hire someone to do this for me! Or maybe I can figure out how to move them all to the regular posts database!

    Anything beyond copying and pasting simple code into a file is out of my league, when it comes to php! lol

    Thanks so much anyway…I appreciate your trying to help!

    Hey, have a great Memorial Day weekend! 🙂

    Plugin Author Michael Beckwith


    I don’t think it’s going to be that drastic of a measure. There are functions in your theme files that control the length of post that get displayed. It’s nothing related to the content in the database. Look for instances of the_content() vs the_excerpt() inside your loops.

    Well I took the real easy way out on this one! I used a plugin to move the custom posts to the regular posts, and it worked like a charm, so I’m good for now!

    When the time comes for more customization, I will have a developer figure this out for me, as I’ll probably have use for custom post types down the road!

    Thanks again Michael!

    Hello guys. I’m kind of having a similar issue? I have a CPT called “reviews” which has 2 taxonomies, “Format” and “Origin”. Under those taxonomies I have created 2 formats and 2 origins, so in the post editor I have a box labeled “Format” which has ‘single’ and ‘album’ as well as a box for “origin” which contains ‘import’ and ‘domestic’.

    Everything is working well, Im calling the CPTs with a WP_query, and echoing out the Taxonomies, however when I click on the ‘single’ or ‘album’ or ‘import’ or ‘domestic’ link, it opens an archive.php page to display the taxonomies.

    I have hierarchy set to ‘true’ on the CPT and the taxonomy. I’ve tried using the above snippet from css-tricks. I’ve renamed the archive.php a ton of various different names like ‘archive-format.php’, ‘archive-format_single.php’ etc etc…

    Anything I’m missing here?

    Also, when I go to the CPT/Taxonomy section in the dashboard I get this error while in debug mode:

    Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/detroitsounds/wp-content/themes/DetroitSounds-theme/functions.php(353) : regexp code on line 1
    Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/detroitsounds/wp-content/themes/DetroitSounds-theme/functions.php(353) : regexp code on line 1
    Popular Format

    Thats while in the ‘Reviews’/’Format’ section…

    Cheers on any ideas…

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘[Resolved] Show custom posts in category archives and in own archives’ is closed to new replies.