Get the page ID if the page content has a shortcode

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(
    array(
        'post_type' = 'page',
    )
  );
  if ( $template_query-have_posts() ) {
    while ( $template_query-have_posts() ) {
        $template_query-the_post();
         $post_content = get_the_content();
        if ( has_shortcode( $post_content, 'availability' ) ) {
            $template_id = get_the_ID();
            //var_dump($template_id);
        }
    }
    wp_reset_postdata();
  }
}
add_action( 'init', 'availabilty_id' );

Topic get-the-id Wordpress

Category Web


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).

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.