i can not access the post in custom template of my wordpress plugin

Question

i added following code to use custom template for my recipes single page

function override_single_template( $single_template ){
    global $post;
    if ($post->post_type == "recipes"){
        $single_template  = plugins_url('/recipe-single-page-template.php',__FILE__);
    }
    return $single_template;
}
add_filter( 'single_template', 'override_single_template',10);

and in my template i added following code

<?php
/*
Template Name: recipe-single-page-template
Template Post Type: recipes
*/
require_once("../../../wp-load.php");
?>

<?php get_header(); ?>

<?php echo $post->ID?>

<?php get_footer(); ?>

but i do not access the post and echo out post id will cuses the following error

Trying to get property of non-object

var dump $post outputs null

NULL

and following code will print out my custom template address

$current_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $current_url;

top code result:

https://charter.test/wp-content/plugins/recipe-plugin/templates/single-recipes.php

now whate should i do?

0
Amir Ramezanpour 4 months 0 Answers 20 views 0

Leave an answer