Creating a block-based theme Category page template for Custom Post Types

I’ve been experimenting with building block-based themes. In order to create a category page template you would create a /block-template directory and add a category.html file. Then you can jump into the block editor, drop in whatever blocks you want to use, go to the code editor, copy the code and paste that into the category.html file.

Now, the key to all of this is that one of the blocks you use when creating the template is the core Query loop block. You’ll then set it to ‘Inherit query from URL’:

That’s it, you’ve got a category page template that displays the posts of whatever category the current url is showing.

The category page template doesn’t work for Custom Post Types

That’s right, I tried using the core Query loop block, but the categories of the custom post type would not display.

Why is that? Because custom post types are excluded from the main query by default. Thank you Pieter Goosen for your very helpful StackOverflow assist.

At any rate, to include the custom post type in the main query, you just need to use the pre_get_posts hook, here’s a code snippet of what that could look like straight from Pieter’s StackOverflow:

function custom_post_type_cat_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_category()) {
      $query->set( 'post_type', array( 'post', 'YOUR CPT' ) );
    }
  }
}

add_action('pre_get_posts','custom_post_type_cat_filter');

112 thoughts on “Creating a block-based theme Category page template for Custom Post Types

  1. Pingback: Google
  2. Pingback: nubain
  3. Pingback: free app for pc
  4. Pingback: cellislim
  5. Pingback: Herbal Incense
  6. Pingback: clitoral vibrator
  7. Pingback: cosmetic box
  8. Pingback: golden bullet
  9. Pingback: pc reparatur jona
  10. Pingback: learn more
  11. Pingback: Cake carts
  12. Pingback: car recycling
  13. Pingback: CZ Shadow 2
  14. Pingback: BERETTA 92X
  15. Pingback: meritroyalbet
  16. Pingback: meritroyalbet
  17. Pingback: meritking
  18. Pingback: madritbet
  19. Pingback: eurocasino
  20. Pingback: madridbet
  21. Pingback: elexusbet
  22. Pingback: Eatery Nanaimo
  23. Pingback: cock sleeves
  24. Pingback: stock market drop
  25. Pingback: anita
  26. Pingback: eurocasino
  27. Pingback: scrap car removal
  28. Pingback: listen here
  29. Pingback: shrooms chocolate
  30. Pingback: Haupia strain
  31. Pingback: Watermelon haupia
  32. Pingback: Platinum haupia
  33. Pingback: Bubble hash
  34. Pingback: Moroccan hash
  35. Pingback: Litto
  36. Pingback: Bubble hash
  37. Pingback: meritroyalbet
  38. Pingback: eurocasino
  39. Pingback: bahis siteleri
  40. Pingback: meritking
  41. Pingback: meritking
  42. Pingback: grandpashabet
  43. Pingback: Grandpashabet

Comments are closed.