Even using poor practices like query_posts() some theme developers make big $ ?

Lately I’ve started branching into using stuff I have liked from themes that I’ve used for child theme implementations in order to improve my ability to provide the content my clients ask me to provide.

And I’m finding out that there are a lot of successful commercial theme developers with themes on WordPress.org that use some poor code
I’ve done several sites that have a home page section of code for displaying the most recent several posts. The code the theme uses is something like this:


<div id="blog">

<div class="someclass">

<h2><?php _e('Blogs on Home Page','domain') ?></h2>

</div>

    <!-- div heading -->
    <?php query_posts('posts_per_page='.get_theme_mod('fp_blog_items', 4)); ?>
    <?php while ( have_posts() ) : the_post() ?>

<div <?php post_class( 'blogitem'); ?> id="post-<?php the_ID(); ?>">
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('home-thumb', array('class' => 'blogimage')); ?></a>

<div class="blogmeta">

<ul>

<li class="date"><?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?></li>


<li class="commentcount"><a href="<?php comments_link(); ?>"><?php comments_number( '0', '1', '%' ); ?></a></li>

</ul>

</div>


<h3 class="blogtitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

        <?php echo wp_new_excerpt('', 15); ?>
        <a href="<?php the_permalink(); ?>" class="bloghome">Read More &raquo;</a>
    </div>

    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
</div>

I found that interesting so I looked up query_posts in the Codex because I hadn’t used it before and found Function Reference/query posts which said “This function isn’t meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).”
Ouch. ? – And that is from a very, very, very successful theme selling company. hehe….
So here is how I did it.First I created a featured-content.php file


<div class="container">

<div class="row">

<h2 style="text-align: center; padding: 1.25rem; color: #6B8887;">Our Latest Blog Posts
        <span style="font-size: .7rem; font-style= italic;">
        a custom loop, using bootstrap for the boxes</span>
        </h2>

        <!--Define our WP Query Parameters-->
        <?php $the_query = new WP_Query( 'posts_per_page=4' ); ?>
        <!--Start our WP Query-->
        <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
        <!--Display the Post Title with Hyperlink-->

<div class="col-lg-3">

<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>

            <!--Display the Post Excerpt-->
            <?php the_excerpt(__('(more…)')); ?>
        </div>

        <!--Repeat the process and reset once it hits the limit-->
        <?php endwhile; wp_reset_postdata(); ?>
    </div>

</div>

Then, I added a small bit of code to the page.php file that is the default for my theme:

<?php if ( is_front_page() ) { // Include the featured content template. get_template_part( 'featured-content' ); } ?>

Another one to file under

Awesome WordPress Tips