Related posts for CPT

How would I go about showing related posts on a single CPT?

To be more specific, in my example, I have a CPT called locations. It has a taxonomy called areas. On the single location post, it should show all the location posts that has the same area assigned to it.

Also - it would be ideal to exclude displaying the current location post (since they are already on that locations post)

Thanks.

Hey Mike! What have you tried so far? Do you have a template you’re working on?

In my testing, this worked for a post

<Loop type=post category=current exclude=current>
       <Field title /> 
</Loop>

But for the CPT, I’m not sure how do to it. For CPT explained in my original post, this was my guess:

<Loop type=location area=current exclude=current>
       <Field title /> 
</Loop>

Try:

<Loop type=location taxonomy=area terms=current>
       <Field title /> 
</Loop>

@PolarRacing maybe I’m wrong, but I thought terms=current only worked when you’re in a taxonomy term loop already. Whereas in this case, the template is going to be on a post so I don’t think terms=current gets the terms from the current post.

I think you’d need to create a list of the taxonomy terms on the current post like this:

<List terms>
  <Loop type=taxonomy_term taxonomy=area post=current>
    <Item><Field name /></Item>
  </Loop>
</List>

And then underneath that you could basically do what you’re suggesting where you’re looping through posts, but you reference the list in that terms parameter instead. Something like this:

<Loop type=location taxonomy=area terms="{Get list=terms}" exclude="{Field id}">
  <Field title />
</Loop>

I think that should do it. @tangiblerun I saw that you wrote exclude=current which might work, but when I check out the docs for the post loop, I’m seeing "exclude - Exclude by ID or name" without any mention of it accepting a value of current. So if it works that’s cool, but inventing stuff in L&L will often get you in trouble so it’s best to stick to only using tags/attributes/values that are mentioned in the documentation. For example, inventing attributes that aren’t written in the docs (like area=current) will never work.

Benjamin, I just needed to change area to areas and it worked. Thank you!

1 Like