Posts Element and Main Query

Hi all,

Already posted in Bricks FB but the forum might be a better place for this type of question?

  • I’ve created a global Archive template to be used on all Posts Type Archives
  • I drop a WP > Posts element in the layout to build the Posts grid
  • Is there a way to set the element source as Main Query so it adapts to the context (can be regular posts, CPT 1, CPT 2…)?

At first glance, in the builder UI - Posts > Content > Query - i can select a Post Type or Any but not Main Query

Maybe this filter could help?

Thanks for your help.

Hi Emmanuel,

Welcome to the forum and a happy new year!

You don’t need to set a source for the posts element on archive templates. The posts element will automatically merge with the main query and fetch all related things and adapt to the context. The only thing you need to set is the template condition to archive and select for which archives you want to show your template (all archives, a specific post type, and so on).

The merge_query filter, which will be a part of Bricks 1.3.7, allows you to exclude specific posts elements getting merged with the main query. For example, if you’re using a posts element in your footer or in a sidebar that shows posts of a specific category, it can happen that it will show “No posts found”, if you’re on an archive page that does not contain posts of the specific category. Please take a look at this post from @Anushka27: How to show a category in the footer when you visit the site

I hope this helps!

Best regards,
timmse

Hi @timmse,

Happy new year to you too, and thanks for the details.

I must have missed something in the Posts Element’s settings > Query section, i can’t find how to let it grab the current Archive as the source.

When i drop a Posts Element in the layout, the query is set to regular Posts by default (named “Articles” in french, please see screenshot below), so it always shows WP regular Posts in frontend, no matter the Archive being currently visited.

I can set the query to “Any” but then the Element displays all post type’s posts: regular or custom post type and pages.

This Posts Element is in an Archive template alright, set to apply on all Archives of the site, including WP regular Posts Archive (“Articles” in french): please see screenshot below.

I also noticed 2 issues with this Archive template and the Posts Element:

  1. The template doesn’t apply to WP regular Posts Archive despite i added “Post Type > Articles” in the conditions (i tried with and without that setting).
    To be mentioned: on my site, the Posts page is set to a specific page named “News” in WP Settings > Reading.

  2. It applies to my CPT alright (CPT name is custom-posts) but i get a 404 when using the Posts pagination.
    URL example: https://bricks.test:7890/custom-posts/page/2/
    I’ve re-saved the permalinks several times, maybe something to look for in the CPT settings, i’m not sure…

Best regards
Emmanuel

Ok, i managed to apply my Archive template to WP regular Posts Archive.
Given that Posts are set to be displayed in the “News” regular page (as the Archive), i added this condition to the template: Individual > News (page)

I get the same 404 on this page as on the CPT Archive when using the Posts Element pagination.
URL example: https://bricks.test:7890/news/page/2/

Which seems to exclude a problem with my CPT, probably more an issue with the Posts Element pagination.

Following: the pagination 404 occurs when the Posts Element query is set to “Any”.
When i switch to a specific Post Type, the pagination works alright.

So i guess the Posts Element is just missing a way to set it to Main query or leave the Query > Post Type blank as suggested by @timmse… except i don’t see a blank option in the dropdown.

Hi Emmanuel,

this is what I have:

  • Regular posts with at least one category applied to each post
  • A custom post type with at least one custom taxonomy applied to each post
  • An archive template with the condition all archives containing a posts element (with the default settings. I didn’t change anything).

When I visit one of the categories (it doesn’t matter if it’s a regular posts category or a custom taxonomy), the archive template kicks in and shows the posts from that specific category. Same with tags, because the archive is set to “all archives”, so it will work for tags as well.

Next, I limited the number of posts to 2 and enabled the posts navigation to test if it worked (and it did).

Then I created a new page, added the posts element (again without touching anything under the query parameters), limited the posts to two again, activated the posts navigation, and made it to my posts page (under settings » reading). So when I visit my posts page, it shows two posts per page, including regular posts and custom posts. The posts navigation is working as well.

I don’t know why you’re getting 404s, but it seems that one or another setting is wrong on your side. Unfortunately, as you’re working locally, I cannot take a look at your settings, so I’d recommend deleting your archive template and recreating it as I have described above.

Btw., I didn’t suggest leaving the post type blank. Instead, I suggested leaving it as it is (set to posts because posts are everything - regular posts, custom post types, even the bricks templates are posts (but they will be excluded by default)).

Best regards,
timmse

Hi @timmse,

Thank you for taking the time to check and detail.

My config is similar to yours: regular WP Posts and Categories, 1 CPT + CT.

I doubled checked my Archive template settings and the Posts Element set in this template layout, still the same output issues in frontend:

  • WP regular Posts page: the Archive template is not applied (unless i set Individual > News (Page) in the template conditions)
  • CPT Archive: the Posts Element displays WP regular Posts instead of the current Post Type posts as it should if the Main Query was used

I think there’s no Main Query available in the Posts Element actually, only a Custom Query set by selecting a Post Type or Any in the Dropdown.
Maybe it will evolve in Bricks 1.3.7 and the Custom Loop Builder, wait and see.

Hi,

Ok this is the correct behavior. The template will not show up on your posts page when it is set to “all archives”, because your posts page is a page - not an archive. So adding another condition for that specific page is totally fine.

This is strange :thinking: How did you create the CPT and Taxonomy? Can you send me the archive template as a json file (via private message and a dropbox link or something similar), so I can take a look at it and test it with my setup?

My CPT and CT are created using the ACF Extended plugin:

Nice of you to propose to check my elements, i’ll contact you in DM.
Please don’t waste too much time on that though. :wink: