Skip to main content
edited title
Link
Mosh
  • 101
  • 3

Show custom post type on post category page doesn't work / breaks naviagationnavigation

Source Link
Mosh
  • 101
  • 3

Show custom post type on post category page doesn't work / breaks naviagation

I'm making some changes to a site created by someone else. I don't work with WP often so I'm not too familiar with all the hooks, etc.
On this site they have normal posts and a custom post type called landingpages. Those landing pages use the same categories as the posts.

We have an overview page of all posts and landingpages together which seems to work fine. On the page we also have a list of all categories. If you click on a category it should show all posts and landingpages that have that category selected.

This is where things go wrong. I can't seem to get those category pages to work the way they should. It works fine when only showing the posts (default behavior) but when I try to add the landingpages in there it shows the posts and landingpages but the navigation disappears.

I found the following code (can't remember which site) which should make it work but in this case the navigation disappears

function test_add_cpt_to_archive_page( $query ) {
    if( (is_category() || is_tag()) && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
        $query->set( 'post_type', 'any');
    }
    return $query;
}
add_filter( 'pre_get_posts', 'test_add_cpt_to_archive_page' );

Also setting the post_type to an array array('post','landingpages') doesn't work either.

The only articles I find on how to do that use the same code snippet as I posted here. Most other articles talk about a custom category page for a custom post type, instead of showing custom post types on the normal category pages.

I'm probably making a rookie mistake here, so any help or feedback would be much appreciated!