• Resolved nando99

    (@nando99)


    I’m using showing the last 5 or so posts from multple categories on a single category page… i’m using the following code like 10 times: https://pastebin.com/UBnVVJRy

    that page seems to load extremely show…

    anyone have a better idea of how to write that code? I tried WP_Query but i couldnt get it to work right… i read about pre_get_posts but i’m not sure how that would work, i know the main code goes in the functions.php but how would i put it on the template…

    any and all help is appreciated

Viewing 9 replies - 1 through 9 (of 9 total)
  • 1. That’s only part of a template. Is that the only query and Loop on the page?

    2. Why are you using tabled markup for presentation? That alone will be causing serious page bloat.

    Thread Starter nando99

    (@nando99)

    full page: https://pastebin.com/MPhWzNPe

    its just the same type of query used multiple times…

    unfortunately, i have to use tables bc we email the end result as a newsletter and tables are the only way to control the message output 100%

    ts just the same type of query used multiple times…

    Well, that’s probably part of the problem. You shouldn’t be using query_posts() multiple times. Only for customising the main Loop – and even then, use pre_get_posts() if you can (though I don’t think it will offer much in the way of a performance improvement).

    However, all of the subsequent queries must use WP_Query() – not query_posts().

    How many separate queries are on that page? I stopped counting when I hit double figures.

    Thread Starter nando99

    (@nando99)

    14 query_posts…

    i tried using wp_query but i couldnt get it to only show posts that below to 2 specific categories… i’m trying to look for the code i used that didnt work…

    Well something like:

    <?php query_posts(array('category__and'=>array(16,4433),'showposts'=>30,'orderby'=>menu_order,'order'=>asc)); if( have_posts() ) : ?>
    [do display stuff]
    <?php endwhile; endif ?>

    which is your second query on that page could be re-written as:

    $args = array(
    	'category__and'=>array(16,4433),
    	'posts_per_page'=>30,
    	'orderby'=>menu_order,
    	'order'=>asc
    );
    $second_query = new WP_Query( $args );
    while ( $second_query->have_posts() ) :
    	$second_query->the_post();>
    	[do display stuff]
    <?php endwhile;
    wp_reset_postdata();

    Note the use of posts_per_page and not showposts which is now deprecated.

    Thread Starter nando99

    (@nando99)

    awesome!

    quick thing, i’d only like to show the header of a section if it has posts… after messing with the code, i got it to show the heading with every post… smh

    <?php
    	$args = array(
    	'category__and'=>array(16,4433),
    	'posts_per_page'=>30,
    	'orderby'=>menu_order,
    	'order'=>asc
    	);
    	$second_query = new WP_Query( $args );
    	while ( $second_query->have_posts() ) : ?>
        <tr>
          <td class="fpb"> News & Upcoming Events</td>
        </tr>
        <?php
    	$second_query->the_post();
    	?>
        <tr>
          <td class="postbit" id="post-<?php the_ID(); ?>"><strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
            <?php the_title(); ?>
            </a></strong>
            <table border="0" cellspacing="0" cellpadding="0" width="100%">
              <tr>
                <td><?php the_excerpt_max_charlength(200); ?></td>
                <?php if (has_post_thumbnail()) {?>
                <td><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><img src="<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 100,100 ), false, '' ); echo $src[0]; ?>" align="right" class="polaroid" style="height:50px; width:50px;" /></a></td>
                <?php } ?>
              </tr>
            </table></td>
        </tr>
        <?php endwhile; wp_reset_postdata(); ?>

    Change:

    while ( $second_query->have_posts() )

    to:

    if( $second_query->have_posts() ) :
    while ( $second_query->have_posts() )

    Then change <?php endwhile; wp_reset_postdata(); ?> to <?php endwhile; endif; wp_reset_postdata(); ?>. That should stop any output from being displayed if there are no posts to display.

    Thread Starter nando99

    (@nando99)

    awesome! thanks!

    Thread Starter nando99

    (@nando99)

    any other way to do this? the page still takes forever to load… about 18 seconds… i have w3 total cache installed as well…

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘need help with queries…’ is closed to new replies.