I have been trying to get the page ID of which it contains shortcode. I 'm getting null. Not sure what I'm doing wrong.

function availabilty_id() {
  global $template_id;
  // Custom Query to look only in pages post type
  $template_query = new WP_Query(
        'post_type' => 'page',
  if ( $template_query->have_posts() ) {
    while ( $template_query->have_posts() ) {
         $post_content = get_the_content();
        if ( has_shortcode( $post_content, 'availability' ) ) {
            $template_id = get_the_ID();
add_action( 'init', 'availabilty_id' );

1 Answer 1


If you want to get the page ID you need to use the $post global. À simple example

function  my_function(){
     global $post;

     $post_id = $post->ID;
     // Etc

Note that it can return a null value for certains types of page (archive).

  • With the above function how you will be able to fetch the ID of a page which contains the shortcode.
    – Maqk
    Commented Nov 9, 2017 at 6:03

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.