Run a Loop Outside of WordPress

Include Basic WordPress Functions

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

Run Loop

<?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; ?>

This can be used on any PHP file even OUTSIDE your WordPress installation.


  1. User Avatar
    Permalink to comment#

    I never seen this earlier when I submitted it…..

    I only wish it was my idea to have this, if not…. Boo Hoo


  2. User Avatar
    Permalink to comment#

    Just noticed it could be mine with the class=”red”

    just to let you guys know, you can make your own classes for the link, I added that for the site I used it on.

    Remember you can change any of the HTML, just don’t change the PHP toooooo much


  3. User Avatar
    Permalink to comment#

    Hi, I’m, trying to use this example on my website but i’m getting this error.

    Fatal error: Call to undefined method stdClass::set_prefix() in /home/../public_html/blog/wp-settings.php on line 268

    I’m using the WP 2.8.4 and this example seems to be out of date, can anyone help me?

    Thanks for your concern.

  4. User Avatar
    Permalink to comment#

    @Mauricio – I am getting this same error message and I have narrowed down the problem but I still don’t understand why the problem exists. Like the post says, I run this code in a PHP file outside of WordPress. If I include the PHP file directly from my site’s template, everything works fine and I don’t get the error. But if I include the file with a function (call a function that includes the file within the function) I get the fatal error.

    Can anyone shed some light on this problem? This seems to be a very common problem according to Google.

  5. User Avatar
    Permalink to comment#

    That’s bad idea. Why don’t you just use


    • User Avatar
      Permalink to comment#

      something went wrong…


      …would probably work better than topic method.

  6. User Avatar
    Mike McKee
    Permalink to comment#

    @Mauricio, I received that oddball…

    Fatal error: Call to undefined method stdClass::set_prefix()

    …if I tried to do the…


    …inside a function. Once I placed it at the top of the PHP page and not in a function, the problem was resolved. Note also you may have to use a virtual path to that file if you try to use it inside a plugin like I was. For instance, in a plugin, the path would be:


    • User Avatar
      Permalink to comment#

      Thanks Mike, I managed to make it work by putting the code on top of the PHP page. So now I get a list of blog posts from WordPress, but now I need to make template for single post view work. I see how to integrate a list of excerpts, but how to integrate single view template? Anybody?

  7. User Avatar
    Piotr Kwiecinski

    Greeting everybody!
    Try to add

    global $wpdb;



    It worked for me. And i hope i’ll help you too.

  8. User Avatar
    Ashvini Saxena
    Permalink to comment#

    Thanks it worked …. I was looking for this code . wonderfully simple..
    My blog

  9. User Avatar
    Permalink to comment#

    Thanks it’s really good. Can you help me to display page navigation (Numbering) at the bottom.

  10. User Avatar
    Ing. Luis Rojas
    Permalink to comment#

    Really helpfull, works pretty nice! Good job, and thanks for share it :)

  11. User Avatar
    Jose Taveras
    Permalink to comment#

    I have 2 blogs within the same database, and i’m running the loop in a ‘custom’ page outside of wordpress. Everything works fine for the first blog but im having trouble with the second blog. Im pretty sure the initial setup was done correctly (not by me) where the first blog has ‘wp_’ as table prefix, the second one has ‘wp2_’.

    The problem arises when i try to pull the second blog into a ‘custom’ Loop. I basically have no idea how to do this.

    I tried using `global $wpdb;$wpdb->set_prefix(‘wp2_’);` but it didnt work as intended.

    I print_r’d $wpdb and indeed the prefix wp2_ is being appended in front of some of the tables, but I’m not really sure if it’s correct.

    Does anyone have any idea?

  12. User Avatar
    Permalink to comment#

    Any idea on how to use this on mulitsite?

    • User Avatar
      Permalink to comment#

      I would like to know the same thing. I would think you could do the same thing with MU

  13. User Avatar
    Manpreet Singh
    Permalink to comment#

    I want to get the single post page outside my wordpress folder.
    Is it possible?

  14. User Avatar
    Permalink to comment#

    Nice, this is exactly what I was looking for. Worked perfectly. Make sure you guys are requiring the right way, using relative URLs.


    That assumes your blog is on the same directory as the file that’s calling the require.

    • User Avatar
      Permalink to comment#

      Ankur, this saved me! Just adding that full-stop before the slash!!
      It was working without it on my local WAMP server!! But upon uploading to the remote server, things went kaput!
      Many thanks!

  15. User Avatar
    Permalink to comment#

    Is there a way to do this so that each post displays in a separate div so that I can style each one differently? I want to display the 3 latest posts in an external website each in their own box with a different background colour.
    Many thanks!

  16. User Avatar
    Permalink to comment#

    how to replace this line for my website?


  17. User Avatar
    Marush Denchev
    Permalink to comment#

    One must not use “query_posts” (it is a requirement from WordPress). One must use pre_get_posts.

  18. User Avatar
    Permalink to comment#

    Hello, it is possible to embed this local code like this site made?

    They use this script to embed the code and show latest post on other website:

    <script type="text/javascript" language="javascript" src=""></script>

    You can try this script or see the print screen to show what they display:

    Are they using the same techniques as yours?

    Thank you in advance, and sorry for my english.

  19. User Avatar

    Your post notes that “This can be used on any PHP file even OUTSIDE your WordPress installation.”

    This is not true is you are attempting to run the loop on a separate site/server.

    For instance, I have a WordPress install @ This code will work if I want to run the loop on, but if I want to run the loop on it will not work, because you can not specify a URL in the server path.

    Does anyone know of a solution for this particular issue?

    I have a single blog that resides at and I want to feed recent posts to both and

    I have managed to add recent posts to using code similar similar to that posted here, but can not figure out how to implement it on a truly external site.

    How can I add recent posts to in a similar fashion?


Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.