Grow your CSS skills. Land your dream job.

Last updated on:

Run a Loop Outside of WordPress

Include Basic WordPress Functions

<?php
  // Include WordPress
  define('WP_USE_THEMES', false);
  require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php');
  query_posts('showposts=1');
?>

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.

Comments

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

    :o)

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

    :o)

  3. 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. 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. Permalink to comment#

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

    Cheers!

    • Permalink to comment#

      something went wrong…

      include(‘wp-config.php’)

      …would probably work better than topic method.

  6. @Mauricio, I received that oddball…

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

    …if I tried to do the…

    require_once('wp-blog-header.php');

    …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:

    require_once('../../../wp-blog-header.php');

    • Petra
      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. Greeting everybody!
    Try to add

    global $wpdb;

    Before

    require_once('wp-blog-header.php');

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

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

  9. Pugazhenthi
    Permalink to comment#

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

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

  11. 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. Any idea on how to use this on mulitsite?

    • Justin
      Permalink to comment#

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

  13. Manpreet Singh
    Permalink to comment#

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

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

      require('./blog/wp-blog-header.php');
    

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

    • Jamesy
      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. Gemma
    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. Tiger
    Permalink to comment#

    how to replace this line for my website?

    ‘/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php’

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

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like this:

```
<div>Example code</div>
```

You don't need to escape code in backticks, Markdown does that for you. If anything screws up, contact us and we can fix it up for you.

*May or may not contain any actual "CSS" or "Tricks".