Archives.php only shows content of type ‘post’, but you can alter it to include custom post types. Add this filter to your functions.php file:
function namespace_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
'post', 'nav_menu_item', 'your-custom-post-type-here'
));
return $query;
}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
Can I make the search to include custom posts too in the results?
Yes.
Put this in your functions.php
I also assume you already added ‘publicly_queryable’ => true, to the $args array of your CPT.
hope it helps
Oh man, I’ve been hacking at this for two days straight! THANK YOU for this simple to implement solution!
THANK YOU! Couldn’t figure this one out!
I’m using the CPT UI tool for custom post types. I assign them a category, but the posts won’t show up.
I can’t figure out how to get this code to work.
My custom post type is called Success Stories and the slug is success-stories.
Can someone help me?
I am having issues in using this snippet. It gives me white screen of depth. I tried on wordpress forums too. But cant get to display custom post types in archives
Thanks for posting this! It worked a treat.
thanks for this code snippets to archived the custom post type.
In the past i used a pluggin (“Simple custom post type archives”) to archive the custom post type and in my theme i had to use a file to display all custom posts type archive.
but now it’s more simple!
thank you again
Can you give a slight bit more detail like…
How does it know to only do this on the archives page? etc….
Its a short snippet so I don’t think it would be much trouble. Thanks in advance.
Solved that problem – getting custom post types to display in an archive page – however mega menu goes blank, some images not displaying on welcome page, welcome page slider menu/nav is not working…
WP3.2.1, Magazinum Theme, Design Chemical Mega Menu, Custom Post Types UI
Thanks but I don’t think this addition to functions.php file will really altar to include custom post types. I agree with Cav. There must be a detailed tutorial.
thanks..
i did it :)
if( is_home() || is_tag() && empty( $query->query_vars[‘suppress_filters’] ) )
Also include ‘nav_menu_item’ if you don’t want your menus to disappear!
Hi and thanks for the code snippet!
I have a theme which displays recent posts on the author.php template and unfortunately nav_menu_items are displayed as posts (with empty content). Without nav_menu_item the navigation menu is gone so I’m looking for something that adds custom post types to my archives without integrating the nav_menu_items anywhere else…
I couldn’t find any solution so far so any help would be highly appreciated!
Cheers,
Thomas
Yes, including “nav_menu_item” was the key for me. Thanks so much, John B.
Thanks Jon. Lifesaver.
Jon, thank you! I used the original snippet, realized my menu had blasted, and couldn’t understand why. Incredibly little information about this–although people are having the same problem–with a cursory Google. I had forgotten where I got the snippet from, happened back on Chris’s page, was like, “Wow. It’s the exact same code. Must be where I ganked it,” then found your comment.
That did the trick. Holy exhausted-but-grateful batman.
Saving grace. Couldn’t figure out why my nav menu’s were disappearing. Thanks.
Thanks a ton, Jon. Saved me a lot of time!
you save me me Jon, thanks a lot.
Thanks, Jon!
Thanks for pointing that out Jon B! Chris you should update your snippet to include Jon’s suggestion.
I’ll update the post, since this is so obviously better. =)
I think this should be resolved by adding
to your conditions
Thanks for the menu tip, Jon B – just what I was looking for.
I have 1 CPT called as “product”. This CPT has unique format/design ( not like regular wordpress posts ) but has same capability ( tags and categories). How can I separate this custom post type in homepage, archive, etc. ???
Thanks, :)
Love it! Worked like a charm.
Thanks guys!
with suppress filters you can affect also the attachment pages
A possible solution
function my_get_posts( $query ) {
if(!is_admin() || is_archive()){
if (!is_post_type_archive() && $query->is_main_query() )
$query->set( ‘post_type’, array( ‘post’, ‘page’, ‘post-format’, ‘video’, ‘slideshow’, ‘timeline’ ) );
return $query;
}
}
!is_admin – without this are affected also the searches in admin pages.
A demo here:
http://www.zurita-bach.com/
At this moment I have only timeline as custom post
Replace $query->$query->is_main_query()
with $query->is_archive()
Thanks Jon B you are the boss!
I’ve found that this creates a problem querying a post_type and a cat.
You can see this in the backend by visiting a category list for a custom post type:
Click the Category menu item in a custom type, then click the number to see the posts in that cat.
wp-admin/edit.php?category_name=some_cat&post_type=some_type
You’ll see the posts displayed seem to be every type BUT the queried one.
Even with Ana’s solution that is supposedly excluding admin.
Hi alvar my final solution
add_filter( ‘pre_get_posts’, ‘my_get_posts’ );
function my_get_posts( $query ) {
if(!is_admin()){
if (!is_post_type_archive() && $query->is_archive())
$query->set( ‘post_type’, array( ‘post’, ‘page’, ‘post-format’, ‘video’, ‘slideshow’, ‘timeline’ ) );
return $query;
}
}
@Ana : Thanks very much, this (&& $query->is_) solved my problem with recent posts widget (before all type post are shown).
I tested every piece of code here and this was the only one that worked perfectly. Thanks Ana!!!
I’ll just put it in a code block for easy copy/paste:
Wow. ANA this just saved my butt. This worked for me.
Thanks Ana!!
Saved my day. Thanks!
I think Ana’s code is getting close to a solution.
The problem on my end is that I have widgets that display custom post types. E.g. “Random Testimonial”, “Latest Tip” etc.
On the pages where this function is being used (the category archive for my custom post type), these widgets are getting confused, and show the content of a different post type – so the random testimonial widget might instead show a random tip.
Any idea what I can do, either to this function or to my widgets, to get them to play nice together?
Thanks for the code…It worked as treat for me…And it is the same i was looking for:)
Ok, my problem was that this was making all my widgets (E.g. latest testimonial) pull the wrong post type.
Here’s the code I solved it with.
This code snippet saved my bacon… one question… how would you include additional arguments like &orderby=title and &order=asc? Thanks!
This worked best for me:
It kept the navigation, my widgets and custom query on the home page intact.
Thanks Daan!
You’re a genious! Thank you so much!
Thanks Daan! This was the fix for me too.
Should have mentioned, I added
!is_admin()
to the conditional so that it wouldn’t pull all my custom post types in the control panel as well as on the archive pages.
Daan’s Solution works perfect.
Works a treat that last post, thanks! However…it now means in the wp-admin, in each custom post list ui, all the different custom post types show up apart from just that one we have clicked on
This same thing happens for me, as well. I’m using Daan’s code and if I can fix this small issue, it’ll be exactly what I need.
Granted I know this comment is about 4 months old, but I did figure out how to fix this issue.
In your functions.php file where you have created your custom post type, change this line:
And that should fix the issue! :) It did on my end anyways, so I hope it helps others!
The code Dann is perfect for troubleshooting with Widgets, but give me a problem when displaying Custom Post Type on the menu if besides “is_category” added “is_tag” solve it this way:
function view_AllCustomPost($query) {
if ($query->is_category) $query->set( ‘post_type’, array(
‘post’, ‘nav_menu_item’, ‘custom_post_type’)
);
}
add_action(‘pre_get_posts’, ‘view_AllCustomPost’);
We were recently tasked with updating a legacy WP site and this thread helped a treat, our navigation items disappeared though so make sure to add “nav_menu_item” back in.
I had better luck modifying the main query.
http://wordpress.stackexchange.com/questions/27104/how-to-display-regular-posts-custom-post-types-that-fall-under-a-category-usin
Ok my above comment did not work after all. Where I have been running into problems is on an archive page for a custom post type. I was able to get everything working with this:
Hope this helps someone, because it was a bear to figure out ; )
Thanks! This worked for me.
Daan’s solution worked for me with WooThemes. I just added the function to my custom plugin file.
Thanks!
Actually, Daan’s code didn’t work properly with my author archive pages so I had to remove ‘nav menu item’ and ‘post’ from the array. My final working code is:
Here’s an example of some alternate syntax—more elegant imo—for adding a custom post type called “lecture” to your taxonomy and date archives:
If you want to include all post types you could use
get_post_types()
to pull all custom post types dynamically:Thanks Alec! This article should ABSOLUTELY be updated with your solution.
You Sir, are a Rockstar!!
this one helped me
`$args = array( ‘posts_per_page’ => 10,’post_type’=>’videos_’, ‘category’ => 3 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<a href="”>
<?php endforeach; `
Glad you found it helpful Ben!
This bit of code saved my life. Thanks a million.
Working on a custom site for the company I work with, using wordpress, but my director wants to have the posts behave like pages, and the pages behave like posts. I have this done so far, but I am [well, if I had hair, I would be… but I digress] pulling my hair out trying to figure out how to make it so that when you click on the menu item that would normally feed off of the archive.php file, and grab the posts, our content is being hosted in pages, so this is showing nothing matches the search,… returning no values.
I have tried all the snippets listed here, to no avail, and am a beginner at best when it comes to php. All I want, is for pages to present within the archive feature, like posts would!
Help!?
Awesome! Thanks!
Hey guys.
Good example!
It’s possible make category.php includes custom post type?
Hugs!
Once again, dude. You totally rock.
Thank you.
Nice tip.
Pretty Neat Trick. Thanks :)
Jon B’s & Daans solutions worked both for me, great! Thx!
Why doesn’t this work out of the box?!? Kept me searching for hours :S
This snippet doesn’t work with WPML. Does anyone have an idea why it doesn’t work on multi-language websites?
thanks,
Bart
I have the same problem… did you come up to a solution? Have you contacted the WPML support team?
Is anyone else facing the same issue ? Does anyone have an idea or a solution ?
Thank you so much for your help, snippet works great without WPML ;)
Emilien
Hi Emilien,
I got this fixed. I also reported this to WPML but they kinda ignored the issue… here’s the thread and how I fixed it: https://wpml.org/forums/topic/wpml-breaks-category-archives-for-custom-post-types/
Hi Bart and thank you for your message,
I finally solved this but in a different way than you did. Here is the snippet I came up to.
Thank you again for your help, have a great day!
Emilien
Thanks fro the post. Is it possible to add items/categories from the custom post types to the default WordPress Archive widget? It only shows posts.
Thanksfor this post, and see menu ppl solve the problem, but i don’t :(
I making my own theme and i successfully make 2 custom post type and i displaying in my blog. Only problem is my arhive.php shows only regular posts, not showing my custom post typs (same after using this code)
can someone tell me where what file i must also check to solve this problem?
Thanks..
I agree with Vlada. Does anyone have a solution that works on the Main Blog, Category pages, and Archive pages? If that was posted, my apologies but I can’t seem to find the solution anywhere.
how to link a custom post page to archive page
The problem with any of the solutions above that I didn’t see mentioned by anyone is the fact that everything works until you get to a custom post type archive. It shows all the posts like a main feed. Here is the final working solution on all WordPress pages. If you see any errors or find something better, let me know :)
I was having problems with the original code causing a problem with the filters on regular posts, “invalid post type”. However, Develocom’s updated snippet here works.
Yes!!!!!!!! After a day and a half, FINALLY something that works for me!
Hello, I’m having the same trouble as everyone else here where my nav menus have disappeared. What’s weird is that everything was fine on my local installation as I was building this theme, but when I installed the theme on a server, that’s when my menus disappeared. If anyone has any insight, I’d love it.
The site is here.
Here is the code I’m using, adapted from this thread:
syntax error elsewhere in my code! Ha!
Hi,
is possible to define or limit a number of posts per page within the same piece of code?
Thanks,
This was very helpful, thanks! It worked for me.
I was trying the different solutions provided and was coming up with some issues, depending on version. The biggest problem was with my masonry layout – anyway I combined ideas and all my problems were solved – no nav menu issue, no masonry layout issue weirdness and all appearing where it needed to be sorry this is long I am just so HAPPY. In case it can help someone else – here it is…
still styling the site a bit but you can see it here
THANK YOU AMY!! I’ve been working on this for way too long now, and your solution was perfect!! I had trouble in various ways with all the rest of the code snippets above. Thank you again for sharing! You’re awesome!! :D
Can somebody help me? I am customizing the search – but my navigation disappears when I do the customization..
found the solution, had to add
$query->is_main_query()
to the if condition, see http://wordpress.stackexchange.com/a/52530Hello,
Widget-archive and widget-calendar are empty if there are only custom posts type (no blog/default posts). I can’t see the month in the archives or click on a day in the calendar if there is not at least one blog post for the period. The page archive is ok (show all posts with the same tag or date)
Is there a way to use the widgets when there are only custom posts?
I try many things.. I use this code :
add_filter( ‘pre_get_posts’, ‘my_get_posts’);
function my_get_posts( $query ) {
if(!is_admin()){
if (!is_post_type_archive() && $query->is_archive())
$query->set( ‘post_type’, array( ‘post’,’portfolio’ ) );
return $query;
}
}
Thank you for this.
To save myself having to manually enter each custom post type, I did the following to automatically add any public post types and it seems to work.
Should make themes easier to maintain as future post types are added.
I tried with all of code. After tried several times… this code is work for me. Here is my code:
Hello Chris,
Here is a plugin I made that can help to create a custom archive of any post_type / taxonomy. You can choose from many base elements like cards, badges, pin-card (Pinterest like layout). Then, you can filter any custom post based on post type, taxonomy, etc. Finally you can make any css changes to your “elements”. All changes are shown in a live preview.
Here is it: http://www.pauple.com/elementary/
OMG. If only I Googled it 2 hours ago…. Thanks!
BTW this code worked for me, didn’t have to modify it just pasted it in my functions.php and voila!
Thanks! This code worked for me!
issue fixed, Thank You
Okay. This code just saved my sanity. Wow. Cannot believe that I did not know this re: archive not displaying CPT. Thank you. Thank you. Thank you.
Thanks for this, but I need to add multiple custom post types, and I can’t figure out the syntax – I guess I’m supposed to add an array of post types?
Any help would be most appreciated!
Thanks so much for these snippets! I knew there was a way to do this, but I had to Bing it to find out. This is still helping folks in 2019.
Hey Team – How do we allow for custom date archive templates for custom post types? Rather than only allot for using date.php, is it possible to prioritize a template like date-mycustomposttype.php?
This no longer seems to work. I had it added to my site using Divi and it worked fine for years. Today I checked and posts are no longer showing in my feeds. Any idea why?
I’m not exactly sure why it wouldn’t still work, but perhaps re-writing the function a bit could help:
Or, if possible, add a
archive-$post-type.php
file in a child theme and add a loop there.