0

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!

2
  • What do you mean by "the navigation disappears"? Do you mean your category list or the main navigation (menu) of your website? Did you try to create an else statement to check if the function is being called correctly?
    – DeltaG
    Commented Sep 1, 2022 at 12:59
  • @DeltaG I mean the main navigation/menu of the site disappears on that page. Not just the menus but the whole header-tag is gone, footer as well. The if-statement seems to be called correctly, only on the category page does it go inside the if-statement, as expected
    – Mosh
    Commented Sep 1, 2022 at 14:09

1 Answer 1

1

The reason the menu is disappearing is because pre_get_posts applies to all queries on the page, including menus, your code is interferring with the menu queries. The is_main_query method should make sure it only affect the query in the loop, which in most cases will be the query displaying the posts.

function test_add_cpt_to_archive_page( $query ) {
    if ( $query->is_main_query() && $query->is_archive() && ! is_admin() ) {
        $query->set( 'post_type', 'any' );
    }
    return $query;
}
1
  • This doesn't seem to work. The menu is still disappearing. I wonder if it's related to the custom post type, though when only normal posts have that category the header doesn't disappear but the footer is still missing some Advanced Custom Fields
    – Mosh
    Commented Sep 5, 2022 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.