How to make specific blog posts accessible by URL but hidden from the blog listing

I want to make a specific blog post publicly accessible by URL, but without it appearing in the blog listing.

For example, I have a blog type called “seminar,” which I output in a custom template called blog-index-seminar.html. This template uses templateType: blog_listing.

Now, for certain seminar posts (type: blog_post), I want them to be accessible if someone knows the URL, but I don’t want them to appear in the front-end listing.

The problem:

  • If I set the post status to “Unpublished,” the URL becomes unnatural and not accessible in the way I want.
  • If I set it to “Archived,” it still appears in the blog listing loop.

As a workaround, I considered creating a static custom module with a boolean field. If the checkbox is enabled on a seminar post, that post would be excluded from the front-end listing. This works, but it creates a new problem: pagination breaks. For example, if the listing is supposed to show 8 posts, it sometimes only shows 5, because excluded posts are still being counted.

The custom module field is called blog_hidden_test. It’s embedded in the seminar blog_post template so editors can toggle visibility on or off per post.

What I’m looking for is:
A simpler or better method to achieve this behavior, without breaking pagination.

{% module "blog_hidden_test" path="/hubspot-demo-theme/modules/blog-hidden-test", label="blog-hidden-test", export_to_template_context=True %}
{% set filtered_contents = contents|selectattr("widgets.blog_hidden_test.body.blog_hidden_true", "ne", true) %}

 <ul class="blog-index-seminar__post-list">
 {% for content in filtered_contents %}
 <li class="blog-index-seminar__post-item">
 <a href="{{ content.absolute_url }}" class="blog-index-seminar__post-link">
	 blog-content
 </a>
 </li>
 {% endfor %}
 </ul>
 {% module "module_17148864296127" path="/my-growth/modules/pagination", label="pagination" %}

I think the most straight-forward solution would be to have your seminars in a separate HubSpot blog.

The alternative would be to give the seminars a specific tag and then filter out that tag in the content loop on the blog listings page.

But that’s liable to get messy as it’ll throw the number of posts in each loop off.


Barry Grennan

Freelance HubSpot CMS Developer

Website | Contact | LinkedIn

Thank you very much. So, in other words, it means preparing a separate blog type dedicated to not being displayed on the front end.

Hi @TMisuna

HubSpot builds the blog listing and pagination before your template code runs, so hiding posts afterwards with HubL filters will always result in broken pagination because those posts have already been counted. To achieve this, assign a special topic like “hidden” to any post you don’t want to appear in the listing, and don’t loop over the pre-paginated contents variable. Instead, use blog_recent_posts() with a limit and offset to pull posts directly from the blog and filter out those tagged “hidden” before displaying them. For example:

{# figure out page and offset #}
{% set posts_per_page = 8 %}
{% set current_page = request.query_dict.page|default(1)|int %}
{% set offset = (current_page - 1) * posts_per_page %}

{# get posts from the same blog but exclude hidden topic #}
{# if your blog has the slug "seminar" and hidden posts are tagged "hidden" #}
{% set all_posts = blog_recent_posts("seminar", posts_per_page * 10, offset=0) %}
{% set visible_posts = all_posts|rejectattr("topic_list", "contains", "hidden") %}
{% set page_posts = visible_posts|slice(offset, posts_per_page) %}

<ul class="blog-index-seminar__post-list">
 {% for content in page_posts %}
 <li class="blog-index-seminar__post-item">
 <a href="{{ content.absolute_url }}" class="blog-index-seminar__post-link">
 {{ content.name }}
 </a>
 </li>
 {% endfor %}
</ul>

{# your pagination module #}
{% module "module_17148864296127" path="/my-growth/modules/pagination", label="pagination" %}

I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.

Thanks!

Thank you very much. Excuse me, but

the way you’re using rejectattr will cause an error:
Invalid argument in ‘rejectattr’: 2nd argument with value contains must be the name of an expression test

and the way you’re using slice will also cause an error:
Invalid argument in ‘slice’: 1st argument with value 0 must be a positive number

Would you like me to also suggest corrected code examples that avoid these errors?