WordPress.org

Ready to get started?Download WordPress

Ideas

Retrieve X number of posts per category via WP_Query

  1. Lasha
    Member

    12345

    Currently, one would need to write a complex and long SQL query to achieve this, as shown here: http://wordpress.stackexchange.com/questions/907/using-wp-query-to-query-multiple-categories-with-limited-posts-per-category

    My proposition, 'posts_per_category' parameter for WP_Query:
    $query = new WP_Query( 'cat=1,2,3,4,5&posts_per_category=5' );

    This query would retrieve the 5 latest posts from each category passed to the 'cat' parameter. In this case, 5 categories with 5 posts each would return 25 posts.

    Ideal Use Cases:
    1) Use the 'posts_per_category' parameter to create a custom archive template showing the latest X number of posts from each specified category.
    2) Custom parse the returned data of WP_Query where you can split up posts into multiple arrays depending on the 'cat_ID', and then individually loop through those arrays to display posts throughout the template.
    3) Can be useful for embedding latest post links for multiple categories in the sidebar, footer, etc.

    Having a 'posts_per_category' option would save developers from having to write DOZENS of lines of custom SQL with a bunch of JOINs, UNONs, LIMITs, etc.

    Posted: 3 years ago #
  2. colinwiseman
    Member

    12345

    I would love this. I had the same problem today where I had to get the latest post from 5 categories.

    I had to do it 5 times, with 5 different loops, to make it clean and tidy.

    If wordpress added a "WP_Union_Query" object this could work as simple as:

    $query = new WP_Union_Query( 'cat=1&post_per_page=5',
    'cat=2&post_per_page=5',
    'cat=3&post_per_page=5',
    'cat=4&post_per_page=5',
    'cat=5&post_per_page=5'
    );

    So 1 query would be built e.g.

    [pseudo code]
    $sql = "SELECT * FROM wp_posts WHERE category_id = 1 LIMIT 0,5
    UNION
    SELECT * FROM wp_posts WHERE category_id = 2 LIMIT 0,5
    UNION
    SELECT * FROM wp_posts WHERE category_id = 3 LIMIT 0,5
    UNION
    SELECT * FROM wp_posts WHERE category_id = 4 LIMIT 0,5
    UNION
    SELECT * FROM wp_posts WHERE category_id = 5 LIMIT 0,5"

    which am sure MySQL can handle without blinking!

    Ta.

    Posted: 3 years ago #
  3. takien
    Member

    12345

    this is very nice idea :)

    Posted: 2 years ago #
  4. Dominik
    Member

    Count me in on this one. I can think of quite a few ocasions where I would have needed this.

    Hope this idea will come true.

    @Lasha: Thanks for the workaround :)

    Posted: 2 years ago #
  5. Ihor Vorotnov
    Member

    12345

    Voted on this, really good idea.

    Posted: 2 years ago #
  6. damukurt
    Member

    12345

    I've tried to do this at least half a dozen times and it annoys me that it isn't this simple! Well done @Lasha

    Posted: 2 years ago #
  7. colorcrate
    Member

    12345

    Absolutely, definitely. I generally accomplish this with multiple loops which is such an ugly solution.

    Posted: 2 years ago #
  8. Jonathan Dingman
    Member

    12345

    +1, sounds like a great idea.

    Posted: 2 years ago #
  9. agence-web-nice
    Member

    12345

    Hello, I agree with this great idea. I vote 5 stars.

    Posted: 1 year ago #
  10. braque
    Member

    I think its really good idea, I definitely need to try this one.

    Posted: 1 year ago #

RSS feed for this topic

Reply »

You must log in to post.

  • Rating

    12345
    59 Votes
  • Status

    This idea is under consideration