0

I am creating a plugin where i have created a custom post type smart-event and I want to serve the single posts under this category via a single page.That's why I have created a single post page named : single-event.php directly into plugin folder -like- event > single-event.php

And I am using this function to load the single page

function myevent($template) {
if ('event' == get_post_type(get_queried_object_id()) && !$template) {
    $template = dirname(__FILE__) . '/single-event.php';
}
return $template;
}
add_action( 'single_template', 'myevent'));

But the single post is being shown in theme single.php page not in the page i have created.

Below is my CPT register code and i have flush the rewrite mode in permalink settings.

function eventCPT(){
    $labels = array(
        'name'               => esc_html__( 'Events',                   'event' ),
        'singular_name'      => esc_html__( 'Event',                    'event' ),
        'menu_name'          => esc_html__( 'Event',                'event' ),
        'name_admin_bar'     => esc_html__( 'Event',                'event' ),
        'add_new'            => esc_html__( 'Add New Event',            'event' ),
        'add_new_item'       => esc_html__( 'Add New Event',            'event' ),
        'new_item'           => esc_html__( 'New Event',                'event' ),
        'edit_item'          => esc_html__( 'Edit Event',               'event' ),
        'view_item'          => esc_html__( 'View Event',               'event' ),
        'all_items'          => esc_html__( 'All Events',               'event' ),
        'search_items'       => esc_html__( 'Search Events',            'event' ),
        'parent_item_colon'  => esc_html__( 'Parent Events:',           'event' ),
        'not_found'          => esc_html__( 'No events found.',         'event' ),
        'not_found_in_trash' => esc_html__( 'No events found in Trash.','event' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'event' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'event' ),
        'capability_type'    => 'post',
        'has_archive'        => false,
        'hierarchical'       => false,
        'menu_position'      => null,
        'menu_icon'          => 'dashicons-calendar-alt',
        'supports'           => array( 'title', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'event', $args );
}

add_action( 'init', 'eventCPT'));
1
  • 2
    I'll guess that $template is not empty and it never passes your if check.
    – Milo
    Commented Jun 12, 2018 at 16:00

1 Answer 1

0

OK, let's try to find this filter in WP code...

In wp-includes/template.php you can find this:

function get_single_template() {
    $object = get_queried_object();

    $templates = array();

    if ( ! empty( $object->post_type ) ) {
        $template = get_page_template_slug( $object );
        if ( $template && 0 === validate_file( $template ) ) {
            $templates[] = $template;
        }

        $name_decoded = urldecode( $object->post_name );
        if ( $name_decoded !== $object->post_name ) {
            $templates[] = "single-{$object->post_type}-{$name_decoded}.php";
        }

        $templates[] = "single-{$object->post_type}-{$object->post_name}.php";
        $templates[] = "single-{$object->post_type}.php";
    }

    $templates[] = "single.php";

    return get_query_template( 'single', $templates );
}

And at the end of get_query_template the filter is applied like so:

return apply_filters( "{$type}_template", $template, $type, $templates );

So, as you can see, the filter is called with some default templates passed as param. And this means that your condition wont be true:

function myevent( $template ) {
    if ('event' == get_post_type(get_queried_object_id()) && !$template) {
        // the part with !$template will be fals, so this code won't run
        $template = dirname(__FILE__) . '/single-event.php';
    }
    return $template;
}
add_action( 'single_template', 'myevent');

Your Answer

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

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