WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] too many wp_querys slowing page down? how to combine? (3 posts)

  1. Bill Weye
    Member
    Posted 6 years ago #

    I have a page that has three different wp_queries, just like this for different categories:

    <?php $my_query = new WP_Query('category_name=next-week&showposts=1');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID; ?>

    It may even increase to 4 or 5 different queries.

    Right now it's taking between 38 and 41 queries to produce the page. Is there a more efficient way to get posts from multiple categories (one post each), then display them on different parts of the page?

    thanks for your help or suggestions in advance.

    Bill

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    query_posts and WP-Query are really meant for modifying an existing query or pulling a lot of posts. Doing that for 1 post is a bit overkill.

    Try using get_posts instead. You'll need to use category ID numbers instead of names though.

    If next-week's category is 3, then you could do this:

    $my_posts = get_posts("category=3&numberposts=1");
    foreach ($my_posts as $post) {
    echo $post->ID;
    echo $post->post_title;
    // and so on, all the columns in the posts table are in $post
    }

    This only uses 1 query.

  3. Bill Weye
    Member
    Posted 6 years ago #

    Thanks, Otto42!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags