I have a custom post type called freebies. I want to use the archive-freebies.php for the child categories also. I have added in my functions.php the following:

function inherit_template() {
    if (is_category()) {
        $catid = get_query_var('cat');
        $cat = &get_category($catid);
        $parent = $cat->category_parent;
        $cat = &get_category($parent);
        if ($cat->cat_ID == 45){
            include (TEMPLATEPATH . '/archive-freebies.php');
add_action('template_redirect', 'inherit_template', 1);

It's working fine so far, it loads the correct archive template, but shows me that there are no posts. What do I have to change? I'd like the template also to display the category name. I looked in the conditional tags, but there are none for custom post types?

This is my achive-freebies.php

<?php get_header(); ?>   
    <section id="main">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
            <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" data-lightbox-type="ajax"><?php the_title(); ?></a></h2>
            <div class="entry">
                <?php the_content(); ?>
            <?php do_quickshare_output(); ?> 
    <?php endwhile; ?>

    <div class="nav-previous alignleft backlink"><?php next_posts_link( 'Older posts' ); ?></div>
    <div class="nav-next alignright rightlink"><?php previous_posts_link( 'Newer posts' ); ?></div>

    <?php else : ?>

        <p><?php _e('Sorry, no posts matched your criteria in freebies.'); ?></p>

    <?php endif; ?>
    <?php get_sidebar('2'); ?>
    <br style="clear:both;" />
<?php get_footer(); ?>

I tried also another template loading function

add_filter( 'template_include', 'freebie_page_template', 99 );

function freebie_page_template( $template ) {

    if ( is_post_type_archive( 'freebies' )  ) {
        $new_template = locate_template( array( 'archive-freebies.php' ) );
        if ( '' != $new_template ) {
            return $new_template ;

    return $template;

But this is not working at all

  • Although it is quite common to find tutorials that use template_redirect filter to include template files, you should avoid that and use template_include filter instead. I think the name is obvious explanatory. Also, why are you are using get_category() with & before? – cybmeta Jun 6 '14 at 12:15
  • Thank you, the function was just a modified answer, so I am not sure about the & :) I will try the template include, although I do not understand how to use it with a custom post type... – Owl Jun 6 '14 at 12:31
  • I tried the new function, I edit my question. But it does not help, now the template is not loading after all. On my first approach the template is loaded, but the wp query is empty, thats the problem – Owl Jun 6 '14 at 12:34
  • After reading again your question and your answer I don't fully understand what you are trying to get. You have a template archive-freebies.php, used for the archive of the custom post type "freebies" and you want to use this template also for what? For all categories that have a parent? For all categories under category with ID = 45? Are the category the core Wordpress category taxonomy? – cybmeta Jun 7 '14 at 8:09
  • Thank you for your comment. Yes its indeed bit complicated. All my CPT "freebies" are in a category called "freebies". When I add child-categories to this, the posts in these child-categories are not anymore displayed in the archive-freebies.php and that was what I wanted. I managed to show them there, but the post query was empty. But I resolved it already, pls refer to the answer below. – Owl Jun 7 '14 at 15:40

I have searched, and searched, and tried, and finally found........

global $wpdb;
$categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE parent = '$cat' ORDER BY name ASC");
foreach($categories as $category) :

<h2><?php echo $category->name; ?></h2>
    <li><a href="<?php echo get_category_link($category->id);?>"><b><?php echo $category->description; ?></b></a>
Posts on this campaign:
<?php global $post;
$myposts = get_posts("category=$category->id");
foreach($myposts as $post) : setup_postdata($post);
- <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endforeach; ?>

<?php endforeach; ?>
| improve this answer | |
  • Could you please explain your answer a bit? I do not understand what you want. Do you mean I should add this code to my archive-freebies.php? It should not get any posts by a taxonomy term, just from the child categories of freebies... ? – Owl Jun 6 '14 at 18:12

Ok, I finally got it. The problem is, that custom post types are not included in the query by default. Which means, that the custom posts are displayed on category and archive pages, but not anymore on the child categories. The have_posts() is just empty.

To include these, I have to add to functions php the solution from here: http://css-tricks.com/snippets/wordpress/make-archives-php-include-custom-post-types/

(pls refer to Jon B's comment if your menu is gone :)

So, by this, I have the custom post type freebies, which are displayed on the archive-freebies.php. The posts from the child category of freebies are displayed in category-freebies.php template when adding this code to functions.php

// make category use parent category template
function load_cat_parent_template($template) {

    $cat_ID = absint( get_query_var('cat') );
    $category = get_category( $cat_ID );

    $templates = array();

    if ( !is_wp_error($category) )
        $templates[] = "category-{$category->slug}.php";

    $templates[] = "category-$cat_ID.php";

    // trace back the parent hierarchy and locate a template
    if ( !is_wp_error($category) ) {
        $category = $category->parent ? get_category($category->parent) : '';

        if( !empty($category) ) {
            if ( !is_wp_error($category) )
                $templates[] = "category-{$category->slug}.php";

            $templates[] = "category-{$category->term_id}.php";

    $templates[] = "category.php";
    $template = locate_template($templates);

    return $template;
add_action('category_template', 'load_cat_parent_template');

I do not fully understand why this works, since I did not manage to display the child-categories posts in the same as the parent and from template hierarchy the category should go before the archive, but the posts are now in a own template, so its ok also. If anybody can suggest a more correct way for it, please!

I have previously tried to change this function to work with archives, but it did not work.

Hope this helps somebody

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.