• Resolved revmt

    (@revmt)


    I’m trying to get the content of the last post by a particular author using php. I didn’t see any easy way through the function reference so I tried this bit of code:

    $array_content=mysql_query("SELECT post_content FROM wp_posts WHERE post_author = $userID");
    $last_content = end($array_content);
    echo $last_content;

    But the result of the sql query isn’t an array as I expected. It gives me some sort of reference instead: “Resource id #58”

    I really don’t understand sql very well, and would appreciate a kick in the right direction.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter revmt

    (@revmt)

    Ok, I figured out that I need to put the query in an array like so:

    $info=mysql_query("SELECT post_content FROM wp_posts WHERE post_author = $userID");
    $array_content = mysql_fetch_array( $info );
    $last_content = end($array_content);
    echo $last_content;

    But this returns the content of the first post by that user even though I use the end() function. Any pointers here?

    You can always try using the plugin “Recent Comments” and you will also need this to make it work. It has many configuration options.

    You can see it in action on this site, on the home page.

    The “recent activity” on the subpages use another plugin called “Get Recent Comments” not to be confused with “Recent Comments” from above. Which may do the trick as well and doesn’t need to post plugin library.

    Thread Starter revmt

    (@revmt)

    Thanks for the links beta, but I actually need the most recent post content by an author (for which I am passing in the user ID as a parameter), and those plugins don’t seem to handle that. This is all for a backend script, so I don’t actually need to display anything.

    $info=mysql_query("SELECT post_content FROM wp_posts WHERE post_author = $userID ORDER BY post_date DESC LIMIT 1");

    You’ll only get one row– the latest post–, so you don’t need to mess with array functions. You probably also want to check post_status and limit to post_status = "publish" unless you want drafts, attachments and some other stuff to be included.

    I’m totally sorry, I read your request too quickly, and jumped the gun on a response.

    Thread Starter revmt

    (@revmt)

    Thanks apljdi! That worked, but I still had to put it in an array and get the last one, or it would return “Resource ID: #59.”

    Here’s the final bit of code in case anyone ever needs to do this:

    $info=mysql_query("SELECT post_content FROM wp_posts WHERE post_author = $userID ORDER BY post_date DESC LIMIT 1");
    $array_content = mysql_fetch_array( $info );
    $last_content = end($array_content);
    echo $last_content;

    What I meant by avoiding array functions is that since you know you’ll only have one row you can do something like this:

    $info=mysql_query("SELECT post_content FROM wp_posts WHERE post_author = 1 ORDER BY post_date DESC LIMIT 1");
    $array_content = mysql_fetch_array( $info , MYSQL_NUM );
    echo $array_content[0];

    That way you avoid the call to end.

    Just for future reference, php’s mysql_query *always* returns a resource. So you will always need to use another function (eg mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_object, mysql_fetch_row, etc.) to get the values.

    Thread Starter revmt

    (@revmt)

    Ah, I didn’t know there was a fetch row. Still new to sql. Thanks.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Getting last post of a user’ is closed to new replies.