- This topic has 10 replies, 4 voices, and was last updated 2 weeks, 6 days ago by Elvin.
December 24, 2021 at 6:14 am #2058442Samuel
I know there is a custom loop feature coming to GPP/GBP, and I’m trying to replicate something similar on a site build I’m working on. I have several spots on the site where I need to provide a list of posts according to a custom query. I can easily do the custom query with code, my question is whether there is any way to use call a Block Content Template using code and use it to display the post information so that the design and layout can be done in Elements with the block editor?
For example, I saw this sample code searching for an answer: https://generatepress.com/forums/topic/cpt-query-block-within-a-page-section/#post-1964135. Would something like this work to use the content template within my own custom loops? Or is there a simpler way to do this? Or will it simply not work at all until the new feature is implemented?
I’m very comfortable writing my own php, hooks, etc and woulud love to give the site admin a way to maintain the look and feel of these loops using the block editor if possible.December 24, 2021 at 6:45 am #2058457DavidStaffCustomer Support
yes, you could use the method provided in that link.
The only thing is, you need to switch the Element Type in the Block element from Content Template to a Hook – then you can set the Custom Hook to the one inside your query. In my example that was
db_custom_post_loopDecember 24, 2021 at 7:07 am #2058469Samuel
Great–and using dynamic data sources will work correctly this way?December 24, 2021 at 7:25 am #2058473DavidStaffCustomer SupportDecember 24, 2021 at 7:28 am #2058474Samuel
Meaning can the block pull the title, date, etc out of the current post record if I set it to pull from the data source rather than a fixed value.December 24, 2021 at 7:34 am #2058478DavidStaffCustomer SupportDecember 24, 2021 at 9:38 am #2058688Samuel
I’m curious. Is there a way to display the block template directly (for example by it’s id) in code instead of assigning the template to a hook? This could make it even more flexible.December 26, 2021 at 8:54 pm #2060195TomLead DeveloperLead Developer
This isn’t possible at this time, unfortunately. We looked at it before David came up with the hook method.
We’ll definitely play with it to see if it’s something we can add as a feature 🙂December 27, 2021 at 5:42 am #2060531Samuel
Any chance when the custom loop feature comes out there will be a filter or other method to allow completely custom queries? For example, a filter on Query Args before the query is run, etc. I have some situations where I’m sure I’ll need to do a custom query to pull the posts but I’m really looking forward to the custom query feature–it will make site maintenance so much easier.December 27, 2021 at 10:20 am #2060960Samuel
I used your sample code and played with it and it works great. One question what elements/css classes should I add to make a grid render correctly? For example, if I wanted the elements to fill a grid that has two columns (50% each)?December 27, 2021 at 7:46 pm #2061256ElvinStaffCustomer Support
Any chance when the custom loop feature comes out there will be a filter or other method to allow completely custom queries? For example, a filter on Query Args before the query is run, etc. I have some situations where I’m sure I’ll need to do a custom query to pull the posts but I’m really looking forward to the custom query feature–it will make site maintenance so much easier.
That’s the goal. But to be specific, I believe this will happen for GenerateBlocks as it is set to be merged with WP Show Posts on Q1 of 2022. 🙂
A wise man once said:
"Have you cleared your cache?"
- You must be logged in to reply to this topic.