title: WordPress - How can I grab the post ID for use in add_rewrite_rule
body: <p>I am trying to grab the ID of the current post, so that I can place it correctly within the rewritten URL.</p>
  
  <pre><code>function custom_rewrite_rule() {
  
      global $post;
      add_rewrite_tag('%type%', '([^&amp;]+)');
  
      $path = 'index.php?p=' . $post-&gt;ID . ''; // This returns NULL so doesn't work.
      add_rewrite_rule('^latest/([^/]*)/([^/]*)?', $path, 'top' );
  
      add_rewrite_rule('^latest/([^/]*)/?', 'index.php?page_id=10&amp;type=$matches[1]', 'top' );
  
  }
  
  add_action('init', 'custom_rewrite_rule', 10, 1);
  </code></pre>
  
  <p>Basically, I have a section, where I list posts called latest, which is then filtered by a 'type' variable (the second param, like 'opinion').</p>
  
  <p><code>/latest/opinion/</code> (successfully loads posts tagged as opinion).</p>
  
  <p><code>/latest/opinion/my-actual-post/</code> (shows blank page, because <code>$post</code> is <code>NULL</code>)</p>
  
  <p>Any help greatly appreciated!</p>