Note that there are some explanatory texts on larger screens.

plurals
  1. POVarnish ESI & Wordpress - simulate loading a post
    text
    copied!<p><strong>Update: I've answered this myself. See below.</strong></p> <p>I guess I'm asking for the equivalent of a <code>set_the_id()</code> function, if one existed.</p> <p>I'm using Wordpress with Varnish for performance, and I want to use an ESI (Edge Side Include) for the sidebar, so things like recent/popular posts can be refreshed more frequently than the post content itself.</p> <p><a href="http://timbroder.com/2012/12/getting-started-with-varnish-edge-side-includes-and-wordpress.html" rel="nofollow">One way of doing this</a> is creating an endpoint in your plugins directory etc. for Varnish to access from an ESI comment, with some code like this:</p> <pre><code>&lt;?php /* ESI handler for Varnish - Sidebar (all tabs except search) */ $cwd = getcwd(); $path = substr($cwd, 0, strpos($cwd, 'wp-content/')); require $path . 'wp-blog-header.php'; echo '&lt;!-- ESI sidebar created '.date(DATE_RFC2822).' --&gt;',"\n"; if ( ! dynamic_sidebar( 'primary-widget-area' ) ) { echo '&lt;!-- Unable to load primary-widget-area (sidebar) --&gt;'; } ?&gt; </code></pre> <p>All fine so far; this is enough to load Wordpress and it to find all the relevant plugins. However they produce errors at various points (specifically <code>wp-includes/post-template.php</code> line 29) because they need to use things like <a href="http://codex.wordpress.org/Function_Reference/get_the_ID" rel="nofollow">get_the_id()</a>, <a href="http://codex.wordpress.org/Function_Reference/in_category" rel="nofollow">in_category()</a> and properties of the post object to highlight certain posts and so on and no post has been loaded yet.</p> <p>How can I tell Wordpress to load a specific post ID in the code above, as if I'd gone to a URL like <code>http://example.com?p=1</code>, but without outputting a full page? I just want the environment to be setup as if post was active, then I can pass the current post ID as a parameter in my ESI call.</p> <p>I thought <a href="http://codex.wordpress.org/Function_Reference/query_posts" rel="nofollow">query_posts()</a> might help and I tried adding:</p> <pre><code>query_posts( 'p=5' ); </code></pre> <p>but it doesn't do anything.</p> <p>I also wondered about <code>WP_Post::get_instance()</code> (it only seems to take an object though, not a post ID). </p> <p>Is there anything else I can use? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload