How to exclude a parent post from a loop of matching taxonomy terms

Hi there, I am hoping this is possible - but I am unable to work out how to do it after looking through the documentation:

  • I have a custom post type called ‘campaign’ (made using ACF)
  • I have a custom taxonomy called ‘link-reference’ (made using ACF) which is assigned to the campaign post type
  • The campaign post type is hierarchical (so I have parent and child posts within this post type)
  • I have, for example, 5 posts that all have the same link-reference term of ‘2024-abc-campaign’. 1 of these is a parent post and the other 4 are child posts

I would like to write a L&L loop which allows me to display all campaign posts with the term of ‘2024-abc-campaign’, but excluding the Parent post (so it will only display the child posts which have that term).

Is this possible?

Thanks very much in advance for your help,

Keith

Maybe you could do this with the parent field and the field attribute? I don’t have time to test this myself unfortunately but the loop might look something like this:

<Loop 
     post_type=campaign 
     taxonomy=link-reference 
     terms=2024-abc-campaign 
     field=parent 
     field_compare=is_not 
     field_value=""
>

This might not work as-is, so maybe you could try looping through all posts, displaying the parent field value for your different posts, and then adjusting the comparison in field_compare and the value in field_value so that the logic makes sense based on what you’re trying to do.

Let me know if you get this working, it’s a pretty interesting use-case!

I looked into this and it turns out field=parent is enough to get all child posts with a parent, and filter out top-level posts.

<Loop post_type=campaign ... field=parent>

It works because the field parent will be empty for any top-level posts, and the default value for field_compare is exists.

1 Like