Display custom post type to category archives
-
First of all thank you for creating the amazing plugin! My question is:
I created a custom post type “trainers” then I enable “categories(WP Core) in “built-in taxonomies” now I add several “trainers” to a certain category. I added the following the my functions.php
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['trainer'] ) ) { $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' );
Base on here https://docs.pluginize.com/article/17-post-types-in-category-tag-archives and in my trainers page template I added the following:
<?php $custom_query = new WP_Query('cat=9'); while($custom_query->have_posts()) : $custom_query->the_post(); ?> <div <?php post_class(); ?> id="post-<?php the_ID(); ?>"> <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> <?php the_content(); ?> </div> <?php endwhile; ?> <?php wp_reset_postdata(); // reset the query ?>
Note that 9 is my category id. It is not displaying the custom post type still. Please point me to the right direction thank you.
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Display custom post type to category archives’ is closed to new replies.