2

I'm trying to create category and tags archive template from my plugin that has custom post type 'product'.

I used the below code to display search results in template file located at plugin_dir_url(__FILE__).'/inc/templates/product-search.php'.

function template_chooser( $template ){

global $post;  

$plugin_root_dir = WP_PLUGIN_DIR.'/product-plugin/';

if( is_search() && get_post_type($post) == 'product' ){

    return $plugin_root_dir.'/inc/templates/domain-search.php';

} else {
    return $template;
}
}

add_filter( 'template_include', 'template_chooser' );

Which loads the search template file perfectly.

But when I try to load the archive-product.php (see below code) file using archive filter archive_template as mentioned in wordpress codex. It simply loads installed theme's archive template with 'No Posts Found'.

below is the code to load archive_template for categories with post type product:

function get_custom_post_type_template( $archive_template ) {
 global $post;
 $plugin_root_dir = WP_PLUGIN_DIR.'/product-plugin/';

 if ( is_post_type_archive ( 'product' ) ) {
      $archive_template = $plugin_root_dir.'/inc/templates/archive-product.php'
 }
 return $archive_template;
}

add_filter( 'archive_template', 'get_custom_post_type_template' ) ;

I tried various answers online but result is same. could you please show me a way to create a custom archive template for a custom post type created inside a plugin?

5
  • Welcome to WordPress Development. I hope you find the answer(s) you are looking for. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. Sep 16, 2019 at 12:03
  • What is $pdm_root_dir in your template path? Or is it a typo?
    – moped
    Sep 16, 2019 at 12:25
  • Apologies. It's a typo. It's been fixed.
    – toddash
    Sep 16, 2019 at 13:06
  • It looks like it should work. Did you verify the path is correct and the template file can be loaded?
    – moped
    Sep 16, 2019 at 14:28
  • Yes i did. Also i used the wordpress templating class here in other file: github.com/GaryJones/Gamajo-Template-Loader/blob/develop/… could that be a problem?
    – toddash
    Sep 16, 2019 at 14:44

1 Answer 1

4

I was able to get it working as desired using the condition is_archive() && get_post_type($post) == 'product'. I also created a custom taxonomy for products as product-category

below is the code:

function get_custom_post_type_template($archive_template){
    global $post;
    $plugin_root_dir = WP_PLUGIN_DIR . '/product-plugin/';

    if (is_post_type_archive('product')){
        $archive_template = $plugin_root_dir . '/inc/templates/archive-product.php';
    }
    return $archive_template;
}
add_filter('archive_template', 'get_custom_post_type_template');

Your Answer

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

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