0

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?

2 Answers 2

1

Instantiate your object inside a function hooked to the template_redirect action:

function wpse_177056_init_shortcode() {
    new callShortCode();
}

add_action( 'template_redirect', 'wpse_177056_init_shortcode' );
2
  • Thanks, it works! How about the performance?
    – Ari
    Feb 4, 2015 at 13:01
  • 1
    Performance? Compared to instantiating immediately? No difference even worth mentioning. Feb 4, 2015 at 13:59
1

The ID of the current object is available only after the actual main query has been executed. Also, get_queried_object_id() uses the global $wp_query object; if this object has not been set up, calling some of its methods or properties won't work.

So, you must use get_queried_object_id() later. According with the codex, the action hook wp is the first available after the WP object has been set up. In your case you can use also wp_footer action directly as it is when you are trying to execute the shortcode:

class callShortCode {

    function __construct() {
        $this->dos();
    }

  function dos() {
        $archive_id = get_queried_object_id();
        //Replace with your logic
        if( $archive_id === 451) {
            $this->show();
        }
   }

   // shortcode call
   function show() {

      $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();
                   echo do_shortcode("[my_shortcode id='5']");
           }
       }

      wp_reset_postdata();

   }
}

add_action( 'wp_footer', function() {
    new callShortCode;
});

Note: I've removed the checing of $pid == $archive_id because actually I don't understand your logic: your using $pid = get_the_ID(); and get_the_ID() should return the same velue of get_queried_object_id() in single post view.

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.