Support » Fixing WordPress » Query posts for Specific Categoty! Please Help

  • Hi!
    Im triyng to get the last 5 post, from my category with id 30 of the current day. How can i complete my query?
    Thanks a lot:

    global $wpdb;

    $today = date(‘Y-m-d’).” 0:00:00″;
    $tomorrow = date(‘Y-m-d’).” 23:59:59″;

    $fivesdrafts = $wpdb->get_results(“SELECT * FROM $wpdb->posts WHERE post_status = ‘publish’ AND post_date BETWEEN \”$today\” AND \”$tomorrow\” ORDER BY post_date ASC”);

Viewing 5 replies - 1 through 5 (of 5 total)
  • Depending upon what you are trying to accomplish exactly, it sounds like you might want to read up on either:

    query_posts or


    The first one is easier, but the second one is more powerful/flexible. Both provide several examples that can serve as excellent starting places.

    Hi Matteo,

    Generally, you don’t want to access the WordPress database directly, you’ll want to use abstraction layers. I can’t really think of a situation where query_posts should be used, although I’m sure it’s theoretically there somewhere.

    Read up on WP_Query as suggested by Andrew Mills, as that’s the preferred method of creating secondary loops. For your example though, I’d just use get_posts(), a simplified wrapper for WP_Query which will return you an array to work with. If you want a real loop with access to template tags, i.e. the_title(), the_author, etc, use WP_Query.


    $args = array('category' => 30, 'numberposts' => 5);
    $myposts = get_posts($args);

    Hi guys,
    thanks to replay me!
    I need to show the events category divided in:

    Today events
    Upcoming Events
    Events passed

    Events it’s a category.
    It’s possible with query_posts to?

    Thanks to helping me!

    I like Andrew Bartel’s suggestion to use get_posts. It has fewer “side effects” than my suggestion to use query_posts.

    If you start with this as your foundation:

    $args = array('category' => 30, 'numberposts' => 5);
    $todayEvents = get_posts($args);

    And read up on the time parameters that get_posts/WP_Query uses, you might come up with something like this for today’s events:

    $today = getdate();
    $args = array('category' => 30, 'numberposts' => 5, 'year=' . $today["year"] . '&monthnum=' . $today["mon"] . '&day=' . $today["mday"]);
    $todayEvents = get_posts($args);

    Start with that and make sure it meets your needs first. Then, go check out the “Return posts from the last 30 days” section of the documentation link I sent you. You’ll see an example there that should help you get the Events Passed part figured out.

    Thanks so much! 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Query posts for Specific Categoty! Please Help’ is closed to new replies.