How to query custom posts using a dynamic category

I’m not sure if this is possible.. I need help as I am probably asking the wrong questions in Google.

What I need to do is:

Pull CPT ‘team’ to a CPT ‘location’ template (I can do this part)

However, each ‘location’ will have a different team, I realise that I can’t code in the category ‘Bristol’ in the query because then my post template will only show ‘Bristol’ ‘Team’

I need it to be user friendly in the CMS as there might be new locations and teams to add.

Any help in the right direction will be much appreciated.

1 Answer
1

If it is a category page, you can get the id of the current category by using get_category:

 $category = get_category( get_query_var( 'cat' ) );
 $cat_id = $category->cat_ID;

EDIT: You could use the variable $cat_id inside your query instead of ‘Bristol’ and it would pull the correct category for each page.

Leave a Comment