Jump to content

Archived

This topic is now archived and is closed to further replies.

skipcollege

Almost working WordPress script - need a little help

Recommended Posts

Hello, I am writing a script that pulls the latest blog post out of a WordPress 2.0.1 database with the status of "publish". The last post's row in the database isn't always published so I've put a loop in the script that is supposed to go to the previous post's db row if the last one wasn't published until it comes to a row that is published.

In my database that I am testing this with the last post is not published. The script goes through the loop once and goes through the if then statement correctly and outputs that it "was not published". However, the loop does not repeat itself, which it should.

I've tried putting quotes around all of the $good variable values...the 0's and 1's. It functions the same either way.

I've also tried changing the equal sign = to a double equal sign == in the while statement while ($good = "0"); to while ($good == "0");. When I make it a double equal sign the script fails from timing out after 60 seconds.

Here is what the script outputs in the browser...

[blockquote]Gets total number of post IDs from certain blog and displays it here: 4

good variable = 0

Resource id #4


Checked the last post ID in the blog and it was not published. That post ID was # 4

Subtracted 1 from the rows variable and is now 3
The good variable is still 0 ...now the loop should repeat.[/blockquote]
Here is my php script code...

[code]
$query = "SELECT ID FROM wp1_posts";
    $result = mysql_query($query);
    if ($result) { $rows = mysql_num_rows($result); } else { mysql_error(); };
    echo("Gets total number of post IDs from certain blog and displays it here: <b>".$rows."</b>");
    echo("<br><br>");
    
    $good = "0";
    
    while ($good = "0");
    {
    
    echo("good variable = ".$good."<br><br>");
    
    $query2 = "SELECT post_status FROM wp1_posts WHERE ID = $rows";
    $result2 = mysql_query($query2);
    echo($result2);
    
    echo("<br><br>");
    if ($result2) { $blob = mysql_fetch_array($result2, MYSQL_ASSOC); } else { mysql_error(); };
    echo($blob);

    echo("<br><br>");
    if ($blob['post_status'] != 'publish')
        {
        echo("Checked the last post ID in the blog and it was not published. That post ID was # ".$rows);
        $rows = ($rows - 1);
        $good = "0";
        echo("<br><br>Subtracted 1 from the rows variable and is now ".$rows);
        echo("<br>The good variable is still ".$good." ...now the loop should repeat.");
        }
    else {
        $good = "1";
        echo("ID #".$rows." post is 'published'");
        echo("<br>The good variable is now ".$good." ...now the loop should be done.");
        };
    
    };
[/code]

It seems like it is just one little thing that I am missing that is causing this to not work! Thanks for any help.

Share this post


Link to post
Share on other sites
I got this solved...

This code works...

[code]    $query = "SELECT *
    FROM wp1_posts
    WHERE post_status = 'publish'
    ORDER BY post_date_gmt DESC
    LIMIT 1";
        $result = mysql_query($query) or die(mysql_error());
        $post = mysql_fetch_array($result);
    
        echo("Gee, the most recent post is:<br>");
        echo '<b>'.$post['post_title'].'</b><br>'.$post['post_content'].'<br><i>Posted On:'.$post['post_date_gmt'].'</i>';
        echo("<br><br>");
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.