Support » Fixing WordPress » WP2.5 problem with have_posts function

  • Resolved Jeff Sherk


    Can somebody test the following code and let me know what the outcome is? I have some custom code that extracts recent posts from different blogs (seperate WP installs) and displays them on a page outside the blog. But since I upgraded to 2.5, the have_posts function returns nothing even though there are posts in the blog. Put this test code in a php file and put in your main blog diretory, and then call it and let me know if anybody gets a “1” back from have_posts:

    $myid=10; //put a post id here that you know is valid
    $mytest = get_post($myid);
    echo "Known good post id ".$myid." returns: ".$mytest->post_title;
    $mytest = have_posts();
    if ($mytest=="") { $mytest="nothing"; }
    echo " and have_posts() returns: ".$mytest;

    The interesting thing is that within ‘the loop’ on my blog have_posts works fine, but from within this program (using wp-blog-header) it does not appear to be working. This code worked fine on WP 2.3

    Thanks to anybody that can test this.

Viewing 15 replies - 1 through 15 (of 16 total)
  • This is my exact problem as well.. Hope there’s a way to get this to work.. Worked fine in 2.3, but then I upgraded to 2.5 and now on the have_post pages just say, “Not Found Sorry, but you are looking for something that isn’t here.

    Scooby, are your blog pages (main posts) working fine?

    have_posts seems to work in ‘the loop’ for me, but not in it’s own seperate program!

    no they are not… I am using the code taken right out of the themes index.php that worked for me on v2.3:

    <?php if (have_posts()) : ?>
    		<?php while (have_posts()) : the_post(); ?>
    			<div class="post" id="post-<?php the_ID(); ?>">
    				<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    				<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    				<div class="entry">
    					<?php the_content('Read the rest of this entry &raquo;'); ?>
    				<p class="postmetadata"><?php the_tags('Tags: ', ', ', ''); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
    		<?php endwhile; ?>
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    	<?php else : ?>
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.
    		<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    	<?php endif; ?>

    It runs through the code and spits out the else statement.. Somethings gotta give here..

    Scooby, I tested your code (had to add require(‘wp-blog-header.php’); at the top) and confirm that it does the same thing on my system… spits out Not Found!!!

    I opened a ticket in the tracker… here is the link:

    I did get that code to spit out the information that it should.. I had to change the permalinks to default.. That code doesn’t work when I customize permalinks.. I would really like to customize permalinks though..

    I have a custom RSS running which had the same problem.

    The Permalinks was set to custom, which I turned to Default and back to custom and it’s working now.

    Strange.. thanks for the help!

    Yes, when I turn the permalinks back to default it starts working again, but when I set the permalinks back to ‘day and name’ it stops working again…

    Something weird here!!!

    This issue has been left open in the tracker for now.

    It is somehow related to the permalinks setting. When set to default, the code below works fine, but when set to custom have_posts returns empty.

    //Displays the title of the 3 most recent posts in the blog
    $posts_per_page = 3;
    if (have_posts()) : while (have_posts()) : the_post();
    ?><br /><?php
    endwhile; else:

    Tracker issue:

    Okay, I determined that the problem is somehow related to the includes/rewrite.php file.

    The following short-term solution worked for me:

    (1) Get a copy of rewrite.php from WP2.3.3 version here:

    (2) Open it in a text editor, and comment out line 903 by putting two slashes (//) in front of it, so the line looks like this:

    (3) In your current WP2.5 install, rename the includes/rewrite.php file to something like includes/rewrite.php.ORIG

    (4) Upload the modified v2.3.3 rewrite.php file to the includes directory.

    (5) Now go to the admin panel in WordPress and choose settings then permalinks. Change the setting back to default and hit save. Now choose your custom permalinks setting and hit save again.

    If the problem is solved great!!!

    If not, go delete the rewrite.php that you uploaded, rename the rewrite.php.ORIG back to rewrite.php, goto permalink settings and change it back to default and hit save.

    Hope this helps somebody!!!

    Just wanted to drop by to say a big thanks to jsherk!

    I’ve been having the same problem with pages not displaying since upgrading to WP2.5, and the rewrite.php trick above worked wonderfully.

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    The long and the short of it is that the rewrite rules changed enough to break your code, because now it can’t figure out what posts you wanted to get in the first place.

    Instead of screwing with the rewrite.php file, try this code:

    //Displays the title of the 3 most recent posts in the blog
    if (have_posts()) : while (have_posts()) : the_post();
    ?><br />
    <?php endwhile; endif; ?>

    When you force the issue with a query_posts call, it should work.

    Obviously, going back to an older version of a file is not the perfect solution, and I don’t know what other problems it may cause, so be sure to check your site (permalinks, rss feeds, etc) to make sure everything else is still working!

    Okay otto42, that did partially solve the problem… I used the query_posts() and put the 2.5 version of rewrite.php back and the code now works with permalinks set custom.

    But now another problem has shown up, which involves multiple installs of WP.

    I have two seperate blog installs at and Note that is outside all the WP installs.

    An external program that tries to pull the posts out of each blog, always gets the blog1 posts, even when it should be getting blog2 posts. Here’s how to recreate:

    Take the following code:

    //Displays the title of the 3 most recent posts in the blog
    if (have_posts()) : while (have_posts()) : the_post();
    ?><br />
    <?php endwhile; endif; ?>

    Save it as BlogOnePosts.php and upload it to directory. Now save the same code again as BlogTwoPosts.php and upload it to directory.

    Go to both and then and you will see that they each correctly display the last three posts for each blog seperately.

    Now take this code:

    Blog 1<br />
    <?php include('/home/public_html/blog1/BlogOnePosts.php'); ?>
    <br /><br />
    Blog 2<br />
    <?php include('/home/public_html/blog2/BlogTwoPosts.php'); ?>

    Save it as BlogPosts.php and upload it to your directory (outside both wordpress installs).

    Now goto

    You will probably get an error message:
    WARNING: require('./wp-blog-header') No Such File Or Directory
    Not sure if this is expected behaviour, but here’s how we fix it…

    In the BlogOnePosts.php change this line:
    to this line:

    Now in the BlogTwoPosts.php change this line:
    to this line:

    Now got back to, and you see that Blog 1 shows the three most recent posts correctly, but Blog 2 shows the three most recent posts from Blog 1 as well instead of it’s own posts.

    Fixed one problem… on to the next one!!!

    jsherk, to pass you over a clue to your problem:

    1. A PHP script can be inside the same directory, another directory or even inside another web-roots directory and even on a remote location. This does not change a thing for PHP.
    2. If you create two times the same file (by copying them), you get two times the same code. This code executed will do two times the same things (well that is what a programming language is for).

    Now take this code:

    Blog 1
    <?php include('/home/public_html/blog1/BlogOnePosts.php'); ?>
    Blog 2
    <?php include('/home/public_html/blog2/BlogTwoPosts.php'); ?>

    Even if one file resides in blog1 and the other in blog2 directory, they still contain the same code and will return the same output if they are executed in the same thread. And thats what the are.

    So the second problem you talk about is one in your program logic.

    If remote files are on (well they should not but this is for demo), you could do it the way you wanted:

    Now take a look at this code:

    Blog 1
    <?php echo file_get_contents('http://blog1.dom/BlogOnePosts.php'); ?>
    Blog 2
    <?php echo file_get_contents('http://blog2.dom/BlogTwoPosts.php'); ?>

    And to keep things clean, please mark this thread as resolved sothat other can benefit from it as well and open a new one with your new problem to get feedback from others. That would be great! I think then more people are able to help.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘WP2.5 problem with have_posts function’ is closed to new replies.