0

I am trying to make something like a web-app with WP. One of the issues I'm facing now is that I cannot find out which page template file my post is looking for. I have looked at template file hierarchy etc. but to no avail. Don't really know which steps to follow from here so hoping there's someone that can help me.

I have made a custom post type called "week". When a week post is published, 7 days of the week, called "palaver" are also generated. These palaver posts are children of the week post and also inherit the custom taxonomy of the published week post. See the code below in my functions.php.

function create_days_week($new_status, $old_status, $post){
if ('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'week'){
    $week = array(
    "0" => 'Saturday',
    "1" => 'Sunday',
    "2" => 'Monday',
    "3" => 'Tuesday',
    "4" => 'Wednesday',
    "5" => 'Thursday',
    "6" => 'Friday'
);

    $latestposts = new wp_query(array(
        'post_type' => 'week',
        'orderby'   => 'post_date',
        'order'     => 'desc',
        ));
    $latestpost = $latestposts->posts;
    $CategoryName = get_the_terms($latestpost[0]->ID, 'area');
    $CatTermID = array($CategoryName[0]->term_taxonomy_id, $CategoryName[1]->term_taxonomy_id);

     foreach ($week as $weekday) {
        $PostID = wp_insert_post(array(
            'post_title'    => $latestpost[0]->post_title . ': ' . $weekday,
            'post_type'     => 'palaver',
            'post_parent'   => $latestpost[0]->ID,
            'post_content'  => ''
        ));
        wp_set_object_terms($PostID, $CatTermID, 'area');
        update_post_meta($PostID, '_wp_page_template', 'single-palaver.php' );      
     }
}} add_action('transition_post_status', 'create_days_week', 10, 3);

So this works fine (took a decent while to figure everything out though haha). When I look at palaver posts in the admin the posts are displayed as "— Week 1: Tuesday — Draft | Parent Page: Week 1". I'm not sure where the leading dash comes from, but since it does not show in the title or anything I have not really bothered to figure it out.

When the status is still draft the URL shows like this "/?post_type=palaver&p=1264&preview=true". The preview page is powered by my single-palaver.php template.

However when I publish the palaver post the url changes to "/palavers/week-1/week-1-thursday/". Which is

[rewrite of slug -> palavers]/[week post]/[palaver post] or

[palaver post]/[parent page/post]/[child page/post].

Post type & taxonomy registration

    register_post_type('palaver', array(
    'supports' => array(
        'title',
        'editor'),
    'rewrite' => array('slug' => 'palavers'),
    'has_archive' => true,
    'taxonomies' => array('area'),
    'public'=> true,
    'menu_icon' => 'dashicons-calendar-alt',
    'hierarchical' => 'true',
    'labels'=> array(
        'name' => 'Palavers',
        'add_new_item' => 'Add new Palaver',
        'edit_item' => 'Edit Palaver',
        'all_items' => 'All Palavers',
        'Singular name' => 'Palaver'
    )
));

Both Week and Palaver are exactly the same apart from name and labels.

register_taxonomy('area', array('week', 'palaver'), array('hierarchical' => true));

What I tried

1. I tried the following template name files:

  • single-palaver-week-palaver.php
  • single.php
  • page.php
  • taxonomy-palaver.php
  • palaver.php

And several variations which seemed plausible.

2. I also tried to set the template file when generating the post:

update_post_meta($PostID, '_wp_page_template', 'single-palaver.php' );  

3. I made a single palaver post which was recognized and after publication loaded from the single-palaver.php file. URL was /palavers/test.

4. I tried to add a filter in functions.php:

add_filter('page_template', function ($template) {
    global $post;
    if ($post->post_parent) {
        $template = locate_template(['single-palaver.php']);
    }
    return $template;
} );

If there is a parent page it could only be the palaver post so this should have worked, but did not.

5. I tried to get the page template slug:

<?php echo esc_html( get_page_template_slug( $post->ID ) ); ?>

However it seems that there are no posts at all to load so this also did not help.. :(


What is shown?

(1) In the "normal published palaver post" with the URL:/palavers/week-1/week-1-thursday/. My header and footer are loaded and it works of the index.php file. However, The Loop does not recognize the current (or any for that matter) post, so nothing is shown beyond that.

(2) When I go to the URL:/palavers something completely strange happens. The Loop recognizes the posts of type "palaver" but works off of 2(!) template files. First it goes through the complete single-palaver.php template file (inc. header and footer), after which it goes through the complete index.php file (again inc. header and footer).

Ideal situation

I'm not sure yet whether I'm going to need an archive for the palaver post type so (2) is not really a priority.

For (1) though I would like all my palaver posts (independent of whether they are children or not) to load the template file single-palaver.php.

I have the feeling that I tried everything but am missing something trivial. If anyone knows an answer or solution to this, or another avenue to explore, it would be greatly appreciated!

Thanks in advance :)

0

If think you were using the wrong filter, please try using 'single_template'.

I assume you have created a plugin to create your custom post type. In the plugin folder you have a "templates" folder where you put the single page template called "palaver_single.php".

function palaver_single_mapping($single) {

global $post;

    if ( $post->post_type == 'palaver' ) {
        if ( file_exists( plugin_dir_path( __FILE__ ) . '/templates/palaver_single.php' ) ) {
            return plugin_dir_path( __FILE__ ) . '/templates/palaver_single.php';
        }
    }

    return $single;
}
add_filter( 'single_template', 'palaver_single_mapping' );

So if a single palaver post is viewed, the template file inside the templates folder will be used.

| improve this answer | |
  • Hi Rank, Thanks for your answer. I did not use a plugin to create my custom post types, my single-palaver.php is in the root of my theme folder. I did try to put the change of filter type to use by changing your code to look for the right templates, however it did not work unfortunately. I think this is due to the fact that the URL "/palavers/week-1/week-1-thursday/". doesn't find any type of post: checking if the post_type is 'palaver' does not work if no posts are found.. I'm going to try rewriting the URL with add_rewrite_rule. Might that work in your opinion? – bramdek May 18 at 10:51

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.