Jim Kulakowski Jim Kulakowski - 9 days ago 5
PHP Question

Wordpress: How do I include custom post types made with Custom Post Type UI (CPT UI) in category archive pages?

I've created four custom post types -- videos, audio, programs, blogs -- with various custom fields and I've managed to created a custom archive page that displays certain custom fields depending on the post type. The archive page is working well when only viewing the archive of a certain custom post type. The issue I'm having is with the archive pages for categories.

Each custom post type has access to global 'post' categories, so all of my categories show up in each post type. I'd like to be able to query a category and show associated posts regardless of the post type. i.e. the 'business' category may pull up two videos, an audio post, and a blog. The problem is that my category pages are empty right now.

Here is my current loop in 'archive.php':

<?php
if ( have_posts() ) : ?>


<div class="container">

<div class="row">

<?php
/* Start the Loop */
while ( have_posts() ) : the_post();

get_template_part( 'template-parts/content-archive', get_post_format() );

endwhile;

the_posts_navigation(); ?>

</div><!-- .row -->

</div><!-- .container -->

<?php endif; ?>


This grabs the template for archive content 'content-archive.php':

<?php
/**
* Get featured posts from Archives
*/

if( is_post_type_archive( 'videos' ) ) {

$video_image = get_field( 'video_still_image' );
print_archive_post($video_image);

} elseif( is_post_type_archive( 'programs') ) {

$program_featured_image = get_field('program_featured_image');
print_archive_post($program_featured_image);

} elseif( is_post_type_archive( 'audio') ) {

$audio_featured_image = get_field('audio_featured_image');
print_archive_post($audio_featured_image);

} elseif( is_post_type_archive( 'blogs') ) {

$blog_featured_image = get_field('blog_featured_image');
print_archive_post($blog_featured_image);

}

?>


And, here is my function from 'functions.php' to build the content for the post:

// Function to print archive type posts
function print_archive_post($image) {

echo '<div class="col-md-4 featured-thumb-container">';
echo '<a href="' . get_the_permalink() . '"><span class="featured-thumb" style="background: url(' . $image . ')"></span></a>';
echo '<h3><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h3>';

// The category list
$post_cats= array();
$categories = get_the_category();

echo '<p class="category-list">';

foreach($categories as $cat) :
echo '<a class="category-link" href="' . get_home_url() . '/category/' . $cat->slug . '">' . $cat->cat_name . '</a><span>,</span> ';
endforeach;

echo '</p>';

echo '</div>';

}


I know I'm missing a conditional to check for category, but I haven't been able to find a solution in my research. Any help would be greatly appreciated. :)

Answer

So, after some research, I found the solution. I needed to add my custom post types to Wordpress's built-in categories and tags. Here is the code that I added to my functions.php file:

// Add custom post types to default WP categories and tags
function add_custom_types_to_tax( $query ) {
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {

        // Get all your post types
        $post_types = get_post_types();

        $query->set( 'post_type', $post_types );
        return $query;
    }
}
add_filter( 'pre_get_posts', 'add_custom_types_to_tax' );

I found the code here: https://premium.wpmudev.org/blog/add-custom-post-types-to-tags-and-categories-in-wordpress/

Additionally, I created a category.php file for category archives only. I used the same code as my archive.php file, but I swapped in a new template file for category content: 'content-category-archive.php'. Here's the code for that template:

<?php
/**
 * Get featured posts from Category Archives 
 */

if( get_post_type() == 'videos' ) {

    $video_image = get_field( 'video_still_image' ); 
    print_archive_post($video_image);

} elseif( get_post_type() == 'programs' ) {

    $program_featured_image = get_field('program_featured_image');
    print_archive_post($program_featured_image);

} elseif( get_post_type() == 'audio' ) {

    $audio_featured_image = get_field('audio_featured_image');
    print_archive_post($audio_featured_image);

} elseif( get_post_type()== 'blogs' ) {

    $blog_featured_image = get_field('blog_featured_image');
    print_archive_post($blog_featured_image);

}

?>

The difference between this and my other 'content-archive.php' template is the conditional for each post type. Instead of checking for is_post_type_archive('my custom post type') I'm now checking the post type for each post in the current category with get_post_type() == 'my custom post type'.

I hope this helps others who may be having the same issue. :)