WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] too many wp_querys slowing page down? how to combine?

[Resolved] too many wp_querys slowing page down? how to combine?

  • 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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Samuel Wood (Otto)

    @otto42

    WordPress.org Tech Dude

    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.

    Thanks, Otto42!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] too many wp_querys slowing page down? how to combine?’ is closed to new replies.