1

fairly new to craftCMS. I've wrestled with this now for hours and exhausted all research avenues.

I have three main "service pages" (Web Design, Digital Marketing, Hosting & Analytics) These are top level Structure pages, each with 5 entries - for example Web Design/Web Development, Web Design/User Experience, Digital Marketing/Inbound, Digital Marketing/SEO, etc. etc.

I have set up an 'entries'field type called 'blogServices' that uses the three main service pages as the parent source, and allows me to select the child pages with no issue. So for instance, if I write a post about Web Development, I can select the 'Web Design/Web Development entry with no problem and have it show up as a related service on the blog post.

Where I am running into a roadblock is I want to pull up 3 most recent blog posts on my 'Web Design' index page. However, I want to be sure that the three most recent posts fall under one of the five 'Web Design' child service entries (side note: Each blog post can also fall under multiple services as well).

Any help with showing me how to filter out only these posts on the index page is of course greatly appreciated. I've been trying the 'relatedTo()' function with no luck at all.

Thank you in advance.

1

You can run a related to query, passing in all of the descendants of the 'parent' structure entry like so (assuming you are on the 'parent' entry page, else you'll need to query that entry first)

// Get the index pages children 
{% set descendants = entry.descendants.all() %}

{% set recentBlogPosts = craft.entries({
  section: blog,
  relatedTo: descendants,
  limit: 3,
  orderBy: 'postDate desc'
}).all() %}

Or, if you don't mind getting blog posts related to the parent page also, you can use the fact that in order to be related to a child page, a blog post must also be related to the parent:

{% set recentBlogPosts = craft.entries({
  section: blog,
  relatedTo: entry,
  limit: 3,
  orderBy: 'postDate desc'
}).all() %}
  • Thank you very much. This solved my challenge! – Jon Smith Aug 19 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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