• I was using this function:
    /**

    • Assign a block template based on post category
      */
      function assign_custom_block_template( $post_id, $post ) {
      // Check if it’s the correct post type
      if ( ‘post’ !== $post->post_type ) {
      return;
      } // Define category-to-template mappings
      $categories_to_templates = [
      ‘motherhood’ => ‘single-motherhood’,
      ‘business’ => ‘single-business’, // Assuming you have a ‘single-business’ template
      ‘lifestyle’ => ‘single-lifestyle’ // Assuming you have a ‘single-lifestyle’ template
      ]; foreach ( $categories_to_templates as $category => $template_slug ) {
      if ( has_category( $category, $post_id ) ) {
      $block_templates = get_block_templates( array( ‘slug’ => $template_slug ), ‘wp_template’ );
      if ( !empty( $block_templates ) ) {
      $custom_template = reset( $block_templates );
      update_post_meta( $post_id, ‘_wp_page_template’, $custom_template->slug );
      break; // Stop the loop after finding and setting a template
      }
      }
      }
      }
      add_action( ‘save_post’, NAMESPACE . ‘\assign_custom_block_template’, 99, 2 );

    To automatically assign a template to a category when selected. It works when I do this, but when I try to update swap templates from let’s say lifestyle to business it updates on the backend but on the front end it still uses the single-lifestyle.html template. I did some debugging and found this:

    [30-Jun-2024 14:08:37 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 241
    [30-Jun-2024 14:08:37 UTC] Intended Template:
    [30-Jun-2024 14:08:37 UTC] Template Saved: wp-custom-template-single-business
    [30-Jun-2024 14:10:57 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 241
    [30-Jun-2024 14:10:57 UTC] Intended Template:
    [30-Jun-2024 14:10:57 UTC] Template Saved: wp-custom-template-single-lifestyle
    [30-Jun-2024 14:10:57 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 241
    [30-Jun-2024 14:10:57 UTC] Intended Template:
    [30-Jun-2024 14:10:57 UTC] Template Saved: wp-custom-template-single-business
    [30-Jun-2024 14:16:01 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 245
    [30-Jun-2024 14:16:01 UTC] Intended Template:
    [30-Jun-2024 14:16:01 UTC] Template Saved: wp-custom-template-single-business
    [30-Jun-2024 14:16:02 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 245
    [30-Jun-2024 14:16:02 UTC] Intended Template:
    [30-Jun-2024 14:16:02 UTC] Template Saved: wp-custom-template-single-lifestyle
    [30-Jun-2024 14:16:09 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 245
    [30-Jun-2024 14:16:09 UTC] Intended Template:
    [30-Jun-2024 14:16:09 UTC] Template Saved: wp-custom-template-single-lifestyle
    [30-Jun-2024 14:16:09 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 245
    [30-Jun-2024 14:16:09 UTC] Intended Template:
    [30-Jun-2024 14:16:09 UTC] Template Saved: wp-custom-template-single-business
    [30-Jun-2024 14:21:12 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 245
    [30-Jun-2024 14:21:12 UTC] Intended Template:
    [30-Jun-2024 14:21:12 UTC] Template Saved: wp-custom-template-single-lifestyle
    [30-Jun-2024 14:21:13 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 245
    [30-Jun-2024 14:21:13 UTC] Intended Template:

    I got frustrated and just did it manually. Judging by my code where am I going wrong? thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • There might be a couple of issues causing the warnings and preventing the template from updating properly on the frontend.

    1. Ensure your categories and template slugs are correctly mapped.
    2. Fix the warning about the undefined array key.
    3. Make sure the meta key _wp_page_template is properly saved.

    You can try this: ?

    /**
    * Assign a block template based on post category
    */
    function assign_custom_block_template($post_id, $post) {
    // Check if it’s the correct post type
    if ('post' !== $post->post_type) {
    return;
    }

    // Define category-to-template mappings
    $categories_to_templates = [
    'motherhood' => 'single-motherhood',
    'business' => 'single-business',
    'lifestyle' => 'single-lifestyle'
    ];

    foreach ($categories_to_templates as $category => $template_slug) {
    if (has_category($category, $post_id)) {
    $block_templates = get_block_templates(['slug' => $template_slug], 'wp_template');
    if (!empty($block_templates)) {
    $custom_template = reset($block_templates);

    // Debugging: check what template is intended and saved
    error_log("Intended Template: $custom_template->slug");

    update_post_meta($post_id, '_wp_page_template', $custom_template->slug);

    // Debugging: confirm what template was actually saved
    $saved_template = get_post_meta($post_id, '_wp_page_template', true);
    error_log("Template Saved: $saved_template");

    break; // Stop the loop after finding and setting a template
    }
    }
    }
    }
    add_action('save_post', 'assign_custom_block_template', 99, 2);

    You can try this code with new hook template_include. This will work definitely.

    function assign_category_template($category_template) {
    if (is_single()) {
    global $post;



    if ('post' !== $post->post_type) {
    return;
    }

    // Define category-to-template mappings
    $categories_to_templates = [
    'motherhood' => 'single-motherhood',
    'business' => 'single-business',
    'lifestyle' => 'single-lifestyle'
    ];

    foreach ($categories_to_templates as $category => $template_slug) {
    if (has_category($category, $post_id)) {
    $category_template = locate_template($template_slug.'.php');
    }
    }

    }
    return $category_template;
    }
    add_filter('template_include', 'assign_category_template');
    • This reply was modified 8 months, 3 weeks ago by mdshak.
    • This reply was modified 8 months, 3 weeks ago by mdshak.
    Thread Starter Caleb Matteis

    (@calebthedev)

    @mdibrahimk48 @mdshak

    Thank you both for your feedback. I apologize for not giving more context. I’m working on an FSE theme. My templates are HTML not PHP. So @mdshak your example renders a blank template on the front end. I changed this line to match the FSE theme:

        foreach ($categories_to_templates as $category => $template_slug) {
    if (has_category($category, $post_id)) {
    $category_template = locate_template($template_slug.'.html');
    }
    }

    The code above rendered a white screen on the front end. @mdibrahimk48 your snippet choose a page template as opposed to a single post template. Essentially I would like for a user to not have to manually choose a template when creating a post for a particular category.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Creating a single-{category}.html template with query block filter’ is closed to new replies.