My WordPress post-type archive page renders archive.php when there are no posts.

When my custom post-type has a pubished post, the archive-post-typeX.php is loaded correctly. However, when there are no posts the rendering hierarchy takes over and archive.php is loaded instead.

The custom post-type is registered in a plugin and I use this code to register the archive template:

function caw_appr_vac_archive_template($caw_appr_vac_template_archive)
{
    global $post;
    $plugin_template_dir = WP_PLUGIN_DIR . '/CAW Apprenticeship Vacancies/templates/';

    if (is_archive() && get_post_type($post) == 'caw_appr_vac') {
        if (file_exists($plugin_template_dir . 'archive-caw-appr-vac-CAW.php')) {
            return $plugin_template_dir . 'archive-caw-appr-vac-CAW.php';
        }
    }
    return $caw_appr_vac_template_archive;
}
add_filter('archive_template', 'caw_appr_vac_archive_template');

How do I prevent this and keep the visitor on the empty archive-post-typeX.php template? I would like to keep the standard functionality for other post-type archive pages and just change it for this specific template.

I tried the add_action('template_redirect', 'function'), but this results in an endless redirect loop and now I am out of ideas.

0

There are 0 best solutions below