Skip to toolbar

Modifying pre_get_posts for Categories & Tags to show Custom Post Types

How to make your custom post types show up on Category & Tags archives

Custom post types are by default excluded from the main query except on taxonomy pages and custom post type archives.

You can simply use pre_get_posts to correctly alter the main query (alter the query variables before the SQL query is built and executed) to your needs.

Just a few notes on pre_get_posts

  • pre_get_posts runs front end and back end queries, so it is very important to do the is_admin() check for queries only meant to run front end or back end
  • pre_get_posts alters all custom instances of WP_Queryget_posts() (which uses WP_Query) and the main query (which also uses WP_Query). You would want to use the is_main_query() check to specifically only alter the main query.

You can do the following in a plugin or your theme’s functions.php (or create a Child Theme and put it in that functions.php):

Some notes:

  • Target WordPress Category archives with is_category()
  • Target WordPress Tags archives with is_tag()
  • You can target other specific queries (like search, 404, etc.) by using those specific is_ functions from WP
  • Target the specific post type you’re wanting to add by including it in the $q->set above. Make sure you do not remove 'post' as this will prevent posts from showing.

From WordPress Stack Exchange: pre_get_posts for Custom Post Types in Categories & Tags

Questions