1

I have a post type that I've registered using "public" => false, "publicly_queryable" => true, because I don't want singles, but I do want these posts to show up on a custom taxonomy archive. With "public" => false, I can't get the posts to appear in the loop on the custom taxonomy archive.

I've tried searching for a solution to this, but I keep coming across "include public and private posts in loop," which isn't exactly what I'm looking for, as I still want the user to be able to set these posts to "private" if they desire.

How can I include a private post type in a loop using the pre_get_posts filter to modify the query?

1 Answer 1

3

Figured it out, it was simpler than I expected.

/**
 * Include catalog posts in archives, even though they're not "public"
 *
 * @param WP_Query $query
 * @return WP_Query
 */
function wpse415668_catalog_posts_in_archives(WP_Query $query): WP_Query {
    if (! $query->is_admin() && $query->is_main_query() && $query->is_tax("catalog_category")) {
        $query->set("post_type", "catalog");
    }

    return $query;
}
add_filter("pre_get_posts", "wpse415668_catalog_posts_in_archives");

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.