Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Jonathan Davies
Jonathan Davies
2,162 Points

Custom Post Types & Tags.

So my problem is i have my regular posts, and project posts (custom posts). What i'm trying to do is pull posts from these two post types that share the same tag(s) and display them.

Also i'm not looking for a specific tag=12 or tag=apples, i'm looking for something more complex. Something that displays what ever tag I happen to click and display those posts.

Any help that doesn't involve a plugin would be appreciated.

3 Answers

Jonathan Davies
Jonathan Davies
2,162 Points

So i fixed this with...

if ( ! function_exists( 'filter_tag' ) ) { function filter_tag($query) { if ($query->is_tag) { $query->set('post_type', array('post', 'portfolio')); }; return $query; }; } add_filter('pre_get_posts', 'filter_tag');

John Locke
John Locke
15,479 Points

How many tags do you expect to end up with? And are there specific filters you want to apply besides the tag links?

The simple way would be alter the Tag template to query for post-type = Post and your Custom Post Types. A less elegant way would be construct a specific page with a specific template for each tag that does the same thing (Query the tag in post-type Post and Custom Post Type).

Jonathan Davies
Jonathan Davies
2,162 Points

Well I made the tag.php and it does indeed get the posts for me, but it wont get posts from the custom post type for some reason.