WordPress.org

Ready to get started?Download WordPress

Forums

Best way to do multiple separate queries? (5 posts)

  1. vcohen
    Member
    Posted 7 years ago #

    I'm building a new WP 2.0.4 blog which needs to support multiple authors plus "staff" content. "Authors" will be 3rd-party users in the "Authors" role, and "Staff" will be our own folks in the "Editor" or "Admin" role. "Authors" can only post to a single category, enforced by Owen Winkler's Limit Categories plugin; "Staff" can post to a variety of categories.

    I need some advice on the most efficient way to structure my home page. My specs call for a number of separate blocks of content:

    1) a block with excerpt from the latest post in Category="News"
    2) a block with excerpt from 2 stories: of the 3rd-part authors, show the latests single post from each of the latest authors to post. I.e., if Tom, Dick and Jane each wrote some posts in this order:

    Tom - post 1, Dick - post 1, Jane - post 1, Jane - post 2

    then display:
    Jane's Post 2
    Dick's Post 1

    3) a block with the latest post in another category
    4) a block with the latest post in yet another category
    5) the excerpt from a specific Page
    6) the excerpt from another specific Page

    and so on.

    Right now, I have a home.php template in which I'm using several techniques:

    I restart the Loop each time I want the latest post in a category, so I have several places on the page with
    <?php query_posts('category_name=news&showposts=1'); while (have_posts()) : the_post(); ?>
    <div id="news">post tags for excerpt, permalink, etc....</div>
    <?php endwhile; ?>

    and for the Page excerpts, I'm using Kafkaesqui's Get-a-post plugin, so I have several calls like:

    <?php if(function_exists('get_a_post')) get_a_post('feature-promo') ?>
    <h1><?php the_title(); ?></h1>
    <?php the_content(); ?>
    <?php edit_post_link(); ?>

    My home currently has 6 query_posts() calls to get the latest post in a specific category, and 3 calls to Get-a-post to get specific Page excerpts -- and I haven't yet implemented the latest-post-by-2-different-authors feature yet.

    Is this the right approach to constructing a page like this?

    WordPress sez it takes 48 queries in 0.3 seconds to build this page. I don't have a sense for how this compares to other pages like this, but it seems like a fairly high number of queries. What;s the best way to implement a page like this? I've looked at a number of plugins that seem to help select the right stuff for the page, incl. Level 10 Blog Matrix, Get-a-Post, Improved-Include Page (seems like that overlaps Get-a-post), and Posts in Category, but I haven't settled on an approach yet. Any advice appreciated!

  2. moshu
    Member
    Posted 7 years ago #

    Well, with so many multiple Loops it is very unlikely you could reduce the number of queries. Yes, the number is high but that's the price to satisfy such complicated requirements.

    Maybe on a production environment you could use the Wp-Cache plugin to reduce the load on the DB.

  3. vcohen
    Member
    Posted 7 years ago #

    Yes, I expected the number was related to the use of multiple loops. I was just wondering if there were any way to get these results with fewer loops. I'm running the loop query to get a single item, several times.

    But it looks like that's just how content retrieval works in WP. If I want a different set of content in a differet area of the page, I have to start a new Loop to get it -- even if I only want one item.

  4. Joshua Sigar
    Member
    Posted 7 years ago #

    If I would to reduce the number of queries, I would construct my own SQL query instead of using WP API (query_posts(), etc).

    Or, this is a joke, get all posts with query_posts and display the posts you want with some mad php skill.

  5. vcohen
    Member
    Posted 7 years ago #

    @alphaoide, I think you're right -- I need to write a plugin for this.

    thanks all for the advice.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags