Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Many searches and tests but in vain, I need an expert to rescue me.

I need a custom post query in worpdress, which returns only 5 posts starting from a specific post. In other terms, there is a div in single.php where i need the list of 5 posts starting from the current post (the post returned by single.php)

I hope I was clear thanks for your valuable help.

share|improve this question
add comment

migrated from stackoverflow.com May 1 '12 at 20:48

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 2 down vote accepted

You can do this by using get_adjacent_post() and the filter get_previous_post_where or get_next_post_where. You'll call get_adjacent_post() as normal, but you'll need to change the LIMIT to 5, using the filter.

I wasn't sure from the question whether or not you wanted to have the current post be first. If you do, you'll need to set the LIMIT to 4 and then use the current postdata to generate the first post.

I would create an array of post ids, and then use get_post() when you iterate through them, as I believe that get_adjacent_post() will return raw rows from the database.

share|improve this answer
    
sorry for the delay in answer, thank you I am going to test this solution starting from now and feedback –  whiteletters and blankspaces May 11 '12 at 17:14
    
Frankly i dont know how to use get-previous_post_where there is no doc on internet, in addition they say this hook is renamed. can u please give me more details if possible, thanks in advance. –  whiteletters and blankspaces May 11 '12 at 23:21
    
The actual hook name is get_{$adjacent}_post_where. Have you gone and actually looked at the source for get_adjacent_post()? It's been quite some time since I wrote this answer, and I did not write it off the top of my head, I had to research it, so it'll probably be about as quick for you to figure it out as it will for me. If you're having a particular issue with it, I can help with that, but I try to avoid just giving code, as I feel it contributes to a lack of understanding in the community. –  m0r7if3r May 12 '12 at 2:51
    
I was needing one year science and experience to understand this. Thank you very much. –  whiteletters and blankspaces Jun 13 '13 at 2:04
add comment

You have to keep resetting the $post variable to advance the loops post counter.

Use something like this:

    global $post;
    get_header(); ?>

    <div id="container">
        <div id="content" role="main">

        <?php
        /* Run the loop to output the post.
         * If you want to overload this in a child theme then include a file
         * called loop-single.php and that will be used instead.
         */
        if(have_posts()) while(have_posts()): the_post();
            the_title();
            for($i = 0;$i < 4;$i++)
            {
                $post = get_next_post();
                setup_postdata($post);
                if(!empty($post))
                {
                    echo '<br/>';
                    the_title();
                    //or whatever code you need to output
                }
                else
                {
                    //no next post found
                }
            }
        endwhile;
        wp_reset_postdata();
        ?>

        </div><!-- #content -->
    </div><!-- #container -->
share|improve this answer
    
sorry for the delay in answer, thank you I am going to test this solution starting from now and feedback –  whiteletters and blankspaces May 11 '12 at 17:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.