• Resolved hancoha

    (@hancoha)


    I’ve been trying to display a list of posts from specific categories and the codex seems easy enough. I’ve tried everything but I can’t seem to get the get_posts or query_posts to work in 2.7. I did a search and noticed some people had a problem with using certain parameters in the query. I can’t even seem to get anything out of either of these functions. Is this a problem with 2.7? Or am I somehow messing up the easiest functions?

Viewing 6 replies - 1 through 6 (of 6 total)
  • stvwlf

    (@stvwlf)

    Hi – it works fine in 2.7 You are perhaps messing it up, but it is not at all the easiest of functions. Please post the segment of your code that is the query_posts statement and the beginning of your loop code.

    Thread Starter hancoha

    (@hancoha)

    I feel like a total idiot. I figured out how to do the query_posts function. Here is my code:

    <?php $category = get_posts('numberposts=5&amp;category=4'); foreach($category as $post): ?>
    <ul>
       <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    </ul>
    <?php endforeach; ?>
    stvwlf

    (@stvwlf)

    Hi

    Just wanting to point out if you figured it out you can’t be a total idiot since this forum is full of people who can’t figure it out and you did!

    Thread Starter hancoha

    (@hancoha)

    I didn’t realize you had to iterate using the foreach loop. If you simply do <?php query_posts(); ?> should you not get all the post data?

    stvwlf

    (@stvwlf)

    Hi

    If you simply do <?php query_posts(); ?> should you not get all the post data?

    No. That is a common misunderstanding. What query_posts does is convert the query parameters you supply into a database query, run the query, retrieve the query output from the database, and convert that output into PHP data structures. You then have to do something WITH the retrieved data. That is what the loop does.

    Thread Starter hancoha

    (@hancoha)

    That makes a lot more sense. I understand that it has to obviously query each post, if there are any, in order for it to return any data. I really appreciate your help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘get_posts and wp_query’ is closed to new replies.