Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post

    Always query certain number of posts

    Hello,

    I need small help with php. This is my code which queries latest 5 post title from database. I would like to query commented and uncommented posts separately and wrote this code:

    PHP Code:
    <?php
            
    global $wpdb;
            
    $posts $wpdb->get_results("SELECT * FROM wp_posts WHERE post_author = $id AND post_type = 'post' LIMIT 5 ");
            foreach (
    $posts as $post) {

            if (
    $post->comment_count >= 1) {
            echo 
    '<li>' $post->post_title '</li>';
                }
            else if (
    $post->comment_count 1) {
            echo 
    '<li>' $post->post_title '</li>';
                }
            } 
            
    ?>
    But there is one big problem. If the latest five posts are all uncommented when I view commented posts it doesn't display anything. I know that it happens because of comparing comments number to 1 and showing posts only after it, but I don't know any better ways.

    Can anyone please help?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    So you always want the most recent 5 commented posts as well as the most recent 5 uncommented posts? I would just remove the LIMIT 5, and use a counter variable in the PHP code as you get a match for either type. Once both types have 5, break out of the loop.

    PHP Code:
            foreach ($posts as $post) {

            if (
    $post->comment_count >= && $postsWithComment 5) {
            
    $postsWithComment++;
            echo 
    '<li>' $post->post_title '</li>';
                }
            else if (
    $post->comment_count && $postsWithoutComment ) {
            
    $postsWithoutComment++;
            echo 
    '<li>' $post->post_title '</li>';
                }
            if (
    $postsWithComment >= && $postsWithoutComment >= 5) {
                break;
            }
            } 
    You can avoid retrieving too much data by fetching rows one at a time and then performing the tests, as opposed to calling a method that retrieves the entire resultset at once.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    What about a union query to take care of this? I think that using a LIMIT on the union will consider each as a resultset (ie: LIMIT 5 limits 5 of each union)? If that's wrong, than it should work on a limit 10 I would suspect.
    Course, I guess that would all be different if the where included a timestamp of somesorts...
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    if ($post->comment_count >= 1) {
            echo 
    '<li>' $post->post_title '</li>';
                }
            else if (
    $post->comment_count 1) {
            echo 
    '<li>' $post->post_title '</li>';
                }
            } 
    Is there any difference in the above two blocks after all? Are you going to echo them in seperate lists?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Quote Originally Posted by Fou-Lu View Post
    What about a union query to take care of this? I think that using a LIMIT on the union will consider each as a resultset (ie: LIMIT 5 limits 5 of each union)? If that's wrong, than it should work on a limit 10 I would suspect.
    Course, I guess that would all be different if the where included a timestamp of somesorts...
    If you could use separate LIMITs on each query in the union, then yeah I'd recommend this too, but LIMIT only applies to what drops out the bottom end of the query, so there would be no way to grab 5 of one and 5 of the other every time.

    Is there any difference in the above two blocks after all? Are you going to echo them in seperate lists?
    That's a good point


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •