Advertisement
arsh999cg

Add default Category or Tags to a CPT

Dec 19th, 2022 (edited)
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.56 KB | None | 0 0
  1. <?php
  2. add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_archives' );
  3.  
  4. function my_cptui_add_post_types_to_archives( $query ) {
  5.     // We do not want unintended consequences.
  6.     if ( is_admin() || ! $query->is_main_query() ) {
  7.         return;
  8.     }
  9.  
  10.     if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
  11.  
  12.         // REPLACE THESE SLUGS WITH THE POST TYPES YOU WANT TO INCLUDE.
  13.         $cptui_post_types = array( 'custom-post-type');
  14.  
  15.         $query->set(
  16.             'post_type',
  17.             array_merge(
  18.                 array( 'post' ),
  19.                 $cptui_post_types
  20.             )
  21.         );
  22.     }
  23. }
  24. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement