You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 with is_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 the post_tag taxonomy 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.
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_tag
taxonomy 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.