-
-
Save kingkool68/a0e9980048908d01426cc3f25ee49e52 to your computer and use it in GitHub Desktop.
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The original gist had
if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');$wp_query->get('tag'would check if the query contains a slug of a tag and if so determines that yes this is a tag page. A better way would be to use[is_tag()](https://developer.wordpress.org/reference/functions/is_tag/)whose purpose is to determines whether the query is for an existing tag archive page.We should also check to make sure the query is the main query and not a secondary loop on the page using
$wp_query->is_main_query(). We also don't want to modify the behavior if the loop is being performed in the admin section of WordPress which we check withis_admin().$wp_query->set('post_type', 'any');would be fine for most situations. It's a good idea to be a little more specific and set the query to look in all of the post types that are associated with thepost_tagtaxonomy which is what is in$taxonomy->object_type.We could go a step further and use
$wp_query->get( 'post_type' );to get all of the current post types being queried and then only adding new post types. Currently if there was another part of the theme or a plugin modifying the query our changes would overwrite their changes.