Created
December 16, 2022 03:38
-
-
Save eminozlem/c4df477fc10bedb8a6d86731c6f44656 to your computer and use it in GitHub Desktop.
Adding all CPTUI post types to the archives.
This file contains 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
#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