Don’t use query posts
ever, my emphasis. It is just a bad function to use
Note: This function isn’t meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
Also, wp_reset_query
should be used for query_posts
. wp_reset_postdata
is meant to be used for WP_Query
and get_posts
, which is the correct functions to use to create custom queries.
For additional info, check out this post I recently done.
get_posts
uses the same parameters as WP_Query
. Whenever you work with taxonomies and terms and in general with a tax_query
, the default field that is used to retrieve the relevant info is term_id
. slug
is also valid and can also be used.
field (string) – Select taxonomy term by (‘term_id'(default) or ‘slug’)
So change $term->name
to either $term->term_id
or $term->slug