Skip to content

Instantly share code, notes, and snippets.

@eminozlem
Created December 16, 2022 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eminozlem/c4df477fc10bedb8a6d86731c6f44656 to your computer and use it in GitHub Desktop.
Save eminozlem/c4df477fc10bedb8a6d86731c6f44656 to your computer and use it in GitHub Desktop.
Adding all CPTUI post types to the archives.
#https://docs.pluginize.com/article/post-types-in-category-tag-archives/
function my_cptui_add_post_types_to_archives( $query ) {
// We do not want unintended consequences.
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$cptui_post_types = cptui_get_post_type_slugs();
$query->set(
'post_type',
array_merge(
array( 'post' ),
$cptui_post_types
)
);
}
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_archives' );
function my_cptui_add_post_types_to_archives( $query ) {
// We do not want unintended consequences.
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
// Replace these slugs with the post types you want to include.
$cptui_post_types = array( 'my_post_type', 'my_other_post_type' );
$query->set(
'post_type',
array_merge(
array( 'post' ),
$cptui_post_types
)
);
}
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_archives' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment