WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WP2.5 problem with have_posts function (17 posts)

  1. jsherk
    Member
    Posted 6 years ago #

    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:

    require('wp-blog-header.php');
    
    $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.

  2. Scooby
    Member
    Posted 6 years ago #

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

  3. jsherk
    Member
    Posted 6 years ago #

    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!

  4. Scooby
    Member
    Posted 6 years ago #

    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;'); ?>
    				</div>
    
    				<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 »'); ?>
    
    			</div>
    
    		<?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>
    		</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..

  5. jsherk
    Member
    Posted 6 years ago #

    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!!!

  6. jsherk
    Member
    Posted 6 years ago #

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

    http://trac.wordpress.org/ticket/6484

  7. Scooby
    Member
    Posted 6 years ago #

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

  8. retardzone
    Member
    Posted 6 years ago #

    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!

  9. jsherk
    Member
    Posted 6 years ago #

    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!!!

  10. jsherk
    Member
    Posted 6 years ago #

    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.

    <?php
    //Displays the title of the 3 most recent posts in the blog
    $posts_per_page = 3;
    require('./wp-blog-header.php');
    if (have_posts()) : while (have_posts()) : the_post();
    the_title();
    ?><br /><?php
    endwhile; else:
    endif;
    ?>

    Tracker issue:
    http://trac.wordpress.org/ticket/6484

  11. jsherk
    Member
    Posted 6 years ago #

    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:
    http://trac.wordpress.org/browser/branches/2.3/wp-includes/rewrite.php?format=raw

    (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:
    //generate_page_uri_index();

    (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!!!

  12. Nick
    Member
    Posted 6 years ago #

    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.

  13. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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:

    <?php
    //Displays the title of the 3 most recent posts in the blog
    require('./wp-blog-header.php');
    query_posts('showposts=3');
    if (have_posts()) : while (have_posts()) : the_post();
    the_title();
    ?><br />
    <?php endwhile; endif; ?>

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

  14. jsherk
    Member
    Posted 6 years ago #

    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!

  15. jsherk
    Member
    Posted 6 years ago #

    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 mydomain.com/blog1 and mydomain.com/blog2. Note that mydomain.com 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:

    <?php
    //Displays the title of the 3 most recent posts in the blog
    require('./wp-blog-header.php');
    query_posts('showposts=3');
    if (have_posts()) : while (have_posts()) : the_post();
    the_title();
    ?><br />
    <?php endwhile; endif; ?>

    Save it as BlogOnePosts.php and upload it to mydomain.com/blog1 directory. Now save the same code again as BlogTwoPosts.php and upload it to mydomain.com/blog2 directory.

    Go to both mydomain.com/blog1/BlogOnePosts.php and then mydomain.com/blog2/BlogTwoPosts.php 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 mydomain.com directory (outside both wordpress installs).

    Now goto mydomain.com/BlogPosts.php

    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:
    require('./wp-blog-header.php');
    to this line:
    require('/home/public_html/blog1/wp-blog-header.php');

    Now in the BlogTwoPosts.php change this line:
    require('./wp-blog-header.php');
    to this line:
    require('/home/public_html/blog2/wp-blog-header.php');

    Now got back to mydomain.com/BlogPosts.php, 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!!!

  16. hakre
    Member
    Posted 6 years ago #

    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.

  17. modifiedcontent
    Member
    Posted 5 years ago #

    I have the same problem. I have WordPress installed in a /blog folder. Including WP posts in the index.php at the root works fine.

    But then when I turn on Permalinks, I get 'Sorry, no posts matched your criteria' on the homepage.

    I see a lot of suggested fixes in this thread, but they all look very messy. Is there an "official" solution?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.