• Resolved ndjworldnews

    (@ndjworldnews)


    Hi;
    Tried posting this message in Wp-Advanced category but doesn’t let me for some reason.

    Question: what is the SQL command I need to use to retrieve the last 4 posts from the database?

    I am using:

    $posts = “SELECT * from $wpdb->posts WHERE post_type=’post’ ORDER BY post_date DESC LIMIT 4”;

    That works, except if I don’t post anything for one day, that command doesn’t pick up anything. How can I do this so that I always have the last four posts, regardless of when I published them?

    Thanks for any suggestions.

    Lode

Viewing 6 replies - 1 through 6 (of 6 total)
  • There’s no reason that query won’t work. You’re not including anything to do with dates, so that query does pull the last 4 posts. I’ve tested it on one of my dev installations, and it works exactly as expected. What result do you got if you run the query directly in phpMyAdmin or the MySQL command line?

    The only thing that I’d change is to add in a filter for post_status so that you don’t get auto-saved revisions, drafts, etc.

    $posts = "SELECT * from $wpdb->posts WHERE post_type='post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 4";

    Thread Starter ndjworldnews

    (@ndjworldnews)

    Thanks for the response.

    Maybe I am doing something wrong elsewhere then.

    Here’s my entire code:
    <?php
    $posts = “SELECT * from $wpdb->posts WHERE post_type=’post’ ORDER BY post_date DESC LIMIT 4”;
    $postX = array();
    $postresult = mysql_query($posts) or die(mysql_error());
    while ($row = mysql_fetch_array($postresult)) {
    $postX[] = $row[0];
    }
    //$ids = shuffle($postX);
    $ids = implode(‘,’, $postX);
    //echo $ids;
    $args = array(
    ‘showposts’=> $number,
    ‘post__in’ => explode(‘,’, $ids),
    ‘orderby’ => ‘rand’,
    );
    query_posts($args); ?>

    What I am trying to accomplish is that the last four posts are displayed in random order each time the website is loaded. But for
    some reason, my code only seems to give me the desired result when
    I post 4 new articles every day. If I skip a day, the result is nothing or 1 or 2 posts are shown.

    Thanks

    Lode

    PS thanks for the suggestion to add the post_status.

    Thread Starter ndjworldnews

    (@ndjworldnews)

    BTW I forgot to add that the variable $number is set to 4.

    Thanks

    Thread Starter ndjworldnews

    (@ndjworldnews)

    Dear catacaustic;

    Hey thanks for the response. I took your suggestion and added the post_status=”publish” option to the query and low and behold that fixed my problem.

    Thanks. I truly appreciate it.

    Lode

    Wow… You’re really doing things the long way. ??

    This will do what you want, and is a whole lot more concise:

    $posts = get_posts (array (
    	"post_per_page" => 4,
    	"post_type" => "post",
    	"orderby" => "date",
    	"order" => "DESC"
    ));
    
    shuffle ($posts);
    
    foreach ($posts as $post) {
        echo '<a href="'.get_permalink ($post->ID).'"><?php echo apply_filters ("the_title", $post->post_title); ?></a>';
    }

    That’s it.

    Thread Starter ndjworldnews

    (@ndjworldnews)

    Hey catacaustic;

    Thank you so very much. Not only does this work, the code is much shorter and my site loads faster too ?? . Thank you. I really do appreciate this.
    Hope I can return the favor one day.
    Have a great one.

    Lode

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Sql command needed’ is closed to new replies.