Fatal error: Call to a member function get_queried_object_id()
I am trying to display shortcode using the following pattern:
class callShortCode {
function __construct() {
global $post;
$this-dos($post);
}
private function dos($post){
global $post;
$pid = get_the_ID();
$archive_id = get_queried_object_id();
if($archive_id == $pid) {
add_action('wp_footer', array($this, 'show'));
}
}
// shortcode call
function show($post) {
$pid = get_the_ID();
$archive_id = get_queried_object_id();
$the_query = new WP_Query( array('post_type' = 'custom_post_type') );
if ($the_query-have_posts()) {
while ($the_query-have_posts()){
$the_query-the_post();
if($pid == $archive_id) {
echo do_shortcode("[my_shortcode id='5']");
}
}
}
}
}
new callShortCode();
But it is returning error like this on the front end:
Fatal error: Call to a member function get_queried_object_id() on a non-object in D:\xxxx\xxxx\xxxxx\wp-includes\query.php on line xx
I have try to wrap it up with init
action but it don't want read the current page id on the front-end and show nothing. I have checked that the $pid = get_the_ID();
and $archive_id = get_queried_object_id();
is returning 0 when I used the init
hook to call the object.
I also wrapped it up using plugins_loaded
but it is returning the same fatal error!
I know it is because it is called too early! But what can I do to make it working correctly?
Topic get-the-id fatal-error hooks Wordpress
Category Web