DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Ever needed to be able to access your WordPress data and run a loop OUTSIDE of your WP install? Here’s a code snippet which allow you to run a WordPress loop on any PHP file, even outside of your WordPress install.

Paste the following code on any PHP file where you want to run your WordPress loop. Don’t forget to modify the following:
line 4: Please specify the path to your WordPress wp-blog-header.php file.
line 5: Query posts using the query_posts() function.

  // Include WordPress
  define('WP_USE_THEMES', false);

<?php while (have_posts()): the_post(); ?>
   <h2><?php the_title(); ?></h2>
   <?php the_excerpt(); ?>
   <p><a href="<?php the_permalink(); ?>" class="red">Read more...</a></p>
<?php endwhile; ?>

Thanks to CSS Tricks for the great tip!



  1. Jean, not expected from you to post functions with deprecated parameters. Already since WordPress version 2.1 (!!!) showposts() has been replaced by posts_per_page() and query_posts()??? Really???

    It has been long ago decided that that is absolutely, unforgivable doing_it_wrong()

  2. Piet you are definitely right. This tip is old and still works “as it”, but I agree with you that no-one should use showposts() in 2013. Recipe has been corrected.

  3. Thanks for the correction Jean.

    I still don’t like the query_posts(), but I do realise that in very few scenarios (and this one will probably be one of those) it actually can work 🙂

  4. For those who would want something different, ie. NOT to use the query_posts, but go a different way by directly using the WPDB class – as seen in example #2 / codex /setup_postdata: https://codex.wordpress.org/Function_Reference/setup_postdata#Example_2

    You could also more or less easily “inject” prepared objects (ie. mimicing the key names of the wp_posts table) into the returned result, which I used to integrate the WP search AND the search results of a shop system by a client of mine… but thats stuff for an entirely different blog post or article 😉

    cu, w0lf.

  5. Hi – not sure what the application for this is (I’m still kinda a newbie when it comes to PHP)

    Does this mean I can embed my posts on websites outside of my wordpress site?

  6. Do you know how to do the same thing with multisite wordpress, this doesn’t work with multisite wordpress????

Leave a Comment

Your email address will not be published. Required fields are marked *