WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] A lot of WP_Query on home page. (11 posts)

  1. halluzineyt
    Member
    Posted 11 months ago #

    I have around 10 WP_Query on home page to display different categories but it seems to eat a lot of my CPU because of mysql. Is there a way to lower this. Im displaying 6 different categories with random posts http://www.flippish.com/.

  2. Shane Gowland
    Member
    Posted 11 months ago #

    If you're determined to run multiple queries against the posts database (to retrieve different categories and randomize results), then naturally, WordPress is going to have to make a lot of database queries.

    The only way to lower the amount of queries is to reduce the complexity of your front page.

  3. andrewmills
    Member
    Posted 11 months ago #

    I think there's another way you could get the same result.

    One wp_query. Multiple loops, each filtering by category. Rewind posts in between loops.

    Start with a WP_Query that retrieves posts from the categories that you want:

    $query = new WP_Query( 'cat=3,6,17,38' );

    And then filter the results based on category with your individual have posts/while have posts loops?

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <!-- See if the current post is in category 3. -->
    <?php if ( in_category('3') ) { ?>
        <!-- and then do your output -->
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <div class="entry">
            <?php the_content(); ?>
        </div>
    <?php } ?>

    And then use rewind_posts to jump back to the start of the query results, and just change the category you test for to 6 on the next loop, 17 on the loop after that, and so on.

  4. halluzineyt
    Member
    Posted 11 months ago #

    Thanks ill try to work on it.

  5. halluzineyt
    Member
    Posted 11 months ago #

    <?php
        $original = new WP_Query();
        $original ->query('cat=12,13,14&showposts=2');?>
    <?php while ($original ->have_posts()) : $original ->the_post(); ?>
    <!-- See if the current post is in category 3. -->
    <?php if ( in_category('12') ) { ?>
    <?php the_content(); ?>
    <?php } ?>
    
    <?php rewind_posts(); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php if ( in_category('13') ) { ?>
    <?php the_content(); ?>
    <?php endwhile; ?>
    
    <?php rewind_posts(); ?>
    <?php if ( in_category('14') ) { ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>
    
    <?php rewind_posts(); ?>
    <?php if ( in_category('15') ) { ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>

    Will it look like this? Will this work?

  6. andrewmills
    Member
    Posted 11 months ago #

    Yes, it will look something like that. You might have HTML in between each category (divs, for example) to handle layout, but that's the basic idea.

    Test it out first on a separate page/template, just to be safe, of course. But based on what I can see, I think that will work.

  7. halluzineyt
    Member
    Posted 11 months ago #

    Hello im having a bit problem with ending the statement

    you called in_category so i have to end it with <?php } ?> but after that the <?php endwhile; endif; ?> is giving me errors

    Parse error: syntax error, unexpected 'endif' (T_ENDIF) in C:\xampp\htdocs\----\----\----\-------\index.php on line 603

    Can you help me on this last one thanks?!

  8. halluzineyt
    Member
    Posted 11 months ago #

    Hello the rewind_posts doesnt seem to be following my first wp_query?

  9. andrewmills
    Member
    Posted 11 months ago #

    It sounds like the number of if openings and closings are mismatched, one too many closings, if I'm not mistaken.

    (You may have already fixed this, though, and I'm just late in responding. If not, could you post the code so I can see it?)

    As far as the rewind_posts issue, what output are you seeing or not seeing?

  10. halluzineyt
    Member
    Posted 11 months ago #

    For the forum and for wordpress community i found a new way i got this from wordpress answers made by a member called G.M

    if functions.php create a function like this:

    function get_posts_for_cat_ordered( $cats = array(), $posts_per_cat = 2 ) {
      if ( empty($cats) ) return false;
      $q = new WP_Query( array('posts_per_page' => 99, 'category__in' => $cats) );
      $posts_ordered = array();
      $done = 0;
      while ( $q->have_posts() ) : $q->the_post();
        global $post;
        $cats = get_the_category();
        $cat = array_shift( $cats );
        if ( ! isset($posts_ordered[$cat->slug]) )
          $posts_ordered[$cat->slug] = array('term' => $cat);
        if ( ! isset($posts_ordered[$cat->slug]['posts']) )
          $posts_ordered[$cat->slug]['posts'] = array();
        if ( count($posts_ordered[$cat->slug]['posts']) == $posts_per_cat ) {
           $done++;
           if ( $done == count($cats) ) return $posts_ordered;
           continue;
        }
        $posts_ordered[$cat->slug]['posts'][] = $post;
      endwhile;
      wp_reset_postdata();
      return $posts_ordered;
    }

    This function take an array of categories and a number of post you want retrieve for every category and return a multidimensional array with posts ordered per category.

    After that use the function just created like so:

    $cats = array(12, 13, 14);
    $posts_per_category = 2;
    $posts_ordered = get_posts_for_cat_ordered( $cats, $posts_per_category );
    
    if ( ! empty($posts_ordered) ) { foreach ( $posts_ordered as $loop_data) {
    
    // Example category heading
      echo '<h2>' . $loop_data['term']->name . '</h2>';
      global $post;
      foreach ( $loop_data['posts'] as $post) {
        setup_postdata($post);
    ?>
    
      <!-- Example post content -->
      <?php the_title(); ?><br />
      <?php the_content(); ?><br />
    
    <?php
      }
      wp_reset_postdata();
    
    } }

    Possible issues:

    If a post has more than one category this code may not worked as expected. But a little change in the get_posts_for_cat_ordered function can solve this issue. However, if you don't need this feature, leave code as is, because is more performant.

    Another issues can happen if in the blog there is no enough posts for each wanted category inside the last 99 posts. This is for the 'posts_per_page' => 99 argument in the query used in the get_posts_for_cat_ordered function. I've used that number because is a reasonable high number, and if the blog has thousands of posts the fucntion will not affect performance.

    Both these issues will not stop the function to work, but the showed posts can be less than expected if the described situations will occur.

    Can you look at it seems to be working on mine.

  11. halluzineyt
    Member
    Posted 11 months ago #

    ill be closing this topic now.

Reply

You must log in to post.

About this Topic