0

I created three plugins that define a custom post type and use template_include to include the template for the single post version.

For some reason, the latest one that I have built creates the records properly, but can't seem to find the template. The code is literally copied-and-pasted, so I'm not sure why this is the case. The only changes were to the names of the functions and post types to avoid conflicts.

Here is the part that adds the template_include filter:

function include_person_template_function($template_path)
{
    if (get_post_type() == 'featured_people') {
        if (is_single()) {
            if ($theme_file = locate_template('single-featured-person.php')) {
                $template_path = $theme_file;
            } else {
                $template_path = plugin_dir_path(__FILE__) . 'single-featured-person.php';
            }
        }
    }
    return $template_path;
}

add_filter('template_include', 'include_person_template_function', 1);

Since the admin works fine, the only other part that could be the problem is where the post type is defined:

function create_featured_people()
{
    register_post_type('featured_people', array(
    'labels' => array(
      'name' => 'Featured People',
      'singular_name' => 'Featured Person',
      'add_new' => 'Add New',
      'add_new_item' => 'Add New Featured Person',
      'edit' => 'Edit',
      'edit_item' => 'Edit Featured Person',
      'new_item' => 'New Featured Person',
      'view' => 'View',
      'view_item' => 'Featured Person',
      'search_items' => 'Search Featured People',
      'not_found' => 'No Featured People Found',
      'not_found_in_trash' => 'No Featured People Found in Trash',
      'parent' => 'Parent Featured Person'
    ),
    'public' => true,
    'menu_position' => 15,
    'supports' => array( 'title', 'editor', 'thumbnail' ),
    'taxonomies' => array( '' ),
    'has_archive' => true
  ));
}

A few other things to note:

  • I have ruled out a caching issue.
  • The other plugins use different function names and CPT names.

Any help or insights would be greatly appreciated!!

  • a copy/paste of your code works for me. is create_featured_people hooked to init? note that there's also the single_template filter that fires for just singular post types, which will simplify your code a bit and not run on every other type of request. – Milo Feb 5 '18 at 18:21
  • @Milo That's really weird. I can't get the template to work in both local and remote WP installations, including a fresh install. Here's the complete code in a Gist: gist.github.com/justinkuepper/c550de53f24895e76dd44c11d8495fc3. Note: I updated the naming to see if it was a "people" to "person" issue. – Justin Feb 5 '18 at 19:26
  • that works for me as well, I can load a template from the theme dir as well as the plugin dir. – Milo Feb 6 '18 at 4:10
0

I am not entirely sure why this is, but I had to go to Settings > Permalinks and then click "Save" without changing anything. I think the file had to be rewritten to account for the new post type?

Regardless, it fixed the issue and I hope someone else can save a lot of time and headache by reading this!

  • Ah yes, I suppose I should have pointed out that rewrite rules need to be flushed after a new post type is added. Just visiting the Permalinks page will flush the rules, you technically don't have to click Save. – Milo Feb 8 '18 at 16:19

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.