If I understood you correctly, you are looking for get_the_terms()
, which..
Retrieves the terms of the taxonomy that are attached to the post.
You could for example have helper functions like these,
// functions.php
function my_sports_post_terms( int $post_id ) : array {
$taxonomies = ['basketball', 'volleyball', 'baseball'];
$terms = [];
foreach ($taxonomies as $taxonomy) {
$terms[$taxonomy] = my_sports_post_tax_terms($post_id, $taxonomy);
}
return $terms;
}
// Force array return value as get_the_terms() may not always return an array
function my_sports_post_tax_terms( int $post_id, string $taxonomy ) : array {
$terms = get_the_terms( $post_id, $taxonomy );
return $terms && is_array($terms) ? $terms : [];
}
And then use the first one in the Loop like so.
// some template file
while ($query->have_posts()) {
$query->the_post();
$terms = my_sports_post_terms( get_the_ID() );
// code..
}
Related Posts:
- Show posts without term
- Why is my WP_Query not working when tax_query terms are an array?
- Get list of posts which have at least one term from a custom taxonomy with WP_Query
- List taxonomy terms plus their latest post ordered by post date
- Get random terms
- WP Query for Posts (Products) in Specific Category that has 2 Specific Tags (*AND* both tags not *OR*)
- Order terms inside a select dropdown
- Get array of current post term ID’s
- How to add terms to my tax_query based off of the current post
- Change default ordering of taxonomy terms – pre_get_terms
- Empty tax_query array returns an empty array
- Pass array of taxonomy terms to wp_query
- Get list of terms that have posts in another term
- Formulate a url to show posts with both taxonomy terms
- Taxonomy search/filter with multiple taxonomies and multiple taxonomy terms
- Order by slug in get_terms with multiple taxonomies
- WordPress Related Post by tags in Single.php
- Function get_queried_object() return NULL in custom taxonomy
- Querying on multiple taxonomies pulled from $_GET checkbox array not working?
- List custom taxonomy terms sharing posts with a term from a second custom taxonomy
- Return Taxonomy/Term Information with Posts (WP_Query/get_posts)
- Get authors by term id or slug
- How to order posts by title after they have already been sorted by category
- WordPress query with items from more than one selfdefined taxonomy as `term` argument
- Taxonomy order exception for specific term
- Filter Custom Taxonomy Posts
- Get posts from multiple tax terms
- How to Order a list of taxonomies? orderby?
- get_the_terms – but only show 4 Posts
- Count posts per taxonomy else change taxonomy if less than x number
- How to add custom meta to ‘pre_get_terms’?
- get term id from term name
- WP_Query to output chosen term and posts with no term assigned
- WP Job Manager plugin – Listing only the twelve job categories on frontpage order by jobs they have
- Getting the post terms ‘wp_get_post_terms’ per post when within the functions.php file
- How to print term name inside wp post loop
- Using WP_Query for categories instead of get_terms
- Check if a post has term inside loop
- Grabbing taxonomy terms and inserting them into an array
- Deleting terms from the WordPress wp terms table
- Can’t get term id for category archive
- How can I do a orderby by the number of items? So basically list by starting with the array with the largest number of post
- How to make a post with certain taxonomy term display first before other post with only one query?
- How can I order a post query’s results based on the number of matching taxonomy terms?
- Only show tag with the same id as the post
- display ACF repater field in archive page
- Save queried result into database
- WP Query by 4 different taxonomies
- Get categories within specific term
- How to display Woocommerce products list by tag
- taxquery taxonomy get terms
- get term objects and post objects in query
- How to show terms from another taxonomy
- How to define a custom hierarchy for terms?
- Add a custom variable to query page object
- How to get several fields from wp_query?
- query only direct child and sub-terms of a current term archive
- How can I get taxonomy term name using term slug & post ID using build in WordPress function or class?
- I need to get all categories from a WP_Query
- taxonomy-{term}.php terms pagination returning 404 after a certain page
- How to query posts based on lat-lng coordinate as post meta?
- How to trigger 404 for custom query var?
- wp_query display posts from same category of the post
- Order posts by meta value and Date
- How to implement time filter to show random post 1 month for one category and 3 months for other categories
- Can you order posts from wp_query by two custom meta fields?
- Pagination working everywhere but the index page
- How do I search inside specific taxonomies in WordPress
- What’s the purpose / logic of split_the_query in WP_Query->get_posts
- How to loop for every result found in the_content() when using the search query?
- if statement in wp_query arguments
- How to build a WP_Query using mulitple tags and using AND or OR operator between them
- Pagination links missing for first link (1) and previous button? How to get pagination links to work?
- wordpress search word, “hello world” ===> ‘hello+world’ ===> ‘hello’, ‘world’
- WP_Query with different postmeta filter for each categories
- Automatically Query Parent Taxonomy
- wordpress ajax relationship query
- Shouldn’t I be able to modify the main query by this filter?
- Get all posts which was posted on X Days WordPress
- Show menus to one admin username
- inserting content of 1 Post to in another with a template hierarchy
- Get the child category ID of current category
- Limit number of posts in WP_query per month
- Use have_posts() with array of post results retrieved by $wpdb->get_results
- how to translate countries output from wp database?
- Custom query does not find pages
- Display posts ONLY from the current logged in user and current week/year/month Elementor posts
- What’s wrong with this meta query? (order by meta key, then title, doesn’t work)
- Display Custom Post Type Based on Taxonomy With WP_Query()
- Change normal grids to masonry grids
- XMLRPC pingback.extensions.getPingbacks not work with parameters
- WordPress pagination not returning posts on second page
- How can I use two custom fields in WP Query
- How can I use a specific wordpress page template if certain words are used in page title [closed]
- wp-query with a tax-query relation of NOT
- Custom query using WP_Query is not working
- Get a list of the last posts grouped by author and filtered by category
- How to replecate the _fields parameter in custom REST api endpoint
- Using WP Query, I want to include all posts in category 1 as long as they are not also in category 2
- Prevent URL Parameter Affecting other WP Query