-1

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' );

1 Answer 1

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

1
  • 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.