0

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. :)

3
  • Why don't you create custom taxonomy for each post type? Commented Nov 20, 2016 at 18:38
  • @ucheng Thanks for your reply! My problem with creating different taxonomies is redundancy. In this case, there would be four 'business' categories -- one for each post type. Is there a way to pull all custom post types related to one category? Commented Nov 20, 2016 at 20:55
  • I add an answer, not sure I understand it correctly. If not, please let me know and I will update the answer. Commented Nov 21, 2016 at 3:01

2 Answers 2

1

Not sure I understand your question correctly. If you want to query all custom post related to one category, you can use WP_Query in the archive page.

  //if you're on a category archive, it will return the category object
  $category_object = get_queried_object();
  $args = array(
      'post_type' => 'your-cpt',
      'cat' => $category_object->term_id
  );

  $wp_query = new WP_Query( $args );

  if ( $wp_query->have_posts() ) {

    while ( $wp_query->have_posts() ) {   
        $wp_query->the_post();
        //echo your posts
    }

  } else {
      // aww, no posts... do other stuff
  }
  wp_reset_postdata();
Sign up to request clarification or add additional context in comments.

Comments

0

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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.