• I have been reading that no one should use query_posts anymore? My site works fine, loads quickly, displays as I want, but I should change it?

    Can anyone out there have a look at the code I’m using and let me know if I should just leave it, or if I need to change it, and if I do, change it to what? Thanks.

    I have 4 sections on my home page, each shows posts from a different category. One section shows 10 news stories, another 1 story with a video, etc.
    I just use this same code 4 times, changing the obvious parameters:

    <?php query_posts('category_name=video&showposts=1;'); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
    <?php the_content(); ?>
    <?php echo do_shortcode( '[hupso]' ); ?>
    <div class="rightdate"><?php the_time('F jS, Y') ?></div>
    <div class="commentlink"><?php comments_popup_link('Comment »', '1 Comment »', '% Comments »'); ?></div>
    <?php the_tags('<p class="post-tags"><strong>Tags:</strong> ', ', ', '</p>'); ?>
    <div class="clear"></div>
    <?php if ( $user_ID ) : ?> <?php edit_post_link(); ?> <?php endif; ?>
    <div class="clear"></div>
    </div>
    <?php endwhile; ?>
    <?php endif; ?>

    Site is at https://rebellionnews.com/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @tanath-fedo,
    There’s isn’t any great issue while using query_posts(); You can update your code if anytime wordpress deprecates the use of this function, otherwise it seems no harm in using this.

    Well, if you still say, we can provide you the code using WP_Query in the very similar manner as you’ve used.
    Thanks!

    Thread Starter Tanath Fedo

    (@tanath-fedo)

    If you have a different code that’s more up to date, I’d like to try it. Thanks.

    Hi @tanath-fedo,
    You can replace your “query_posts();” code by the following lines of code.
    Hope it will serve your purpose!

    <?php
    $args = array(
        'post_type' => 'post',
        'category' => 'video',
        'posts_per_page' => -1,
    );
    $result = new WP_Query( $args );
    $posts = $result->posts;
    
    if( !empty( $posts ) ) {
        foreach( $posts as $post ) {
            ?>
            <div <?php post_class();?> id=post-"<?php echo $post->ID;?>">
                <h3>
                    <a href="<?php echo get_permalink( $post->ID );?>"><?php echo $post->post_title;?></a>
                </h3>
                <p>
                    <?php echo $post->post_content;?>
                </p>
                <?php echo do_shortcode( '[hupso]' );?>
                <div class="rightdate"><?php the_time('F jS, Y') ?></div>
            </div>
            <div class="commentlink">
                <?php comments_popup_link('Comment ?', '1 Comment ?', '% Comments ?'); ?>
            </div>
            <strong>
                <?php $tags = wp_get_post_tags( $post->ID );?>
                <?php foreach( $tags as $tag ) {?>
                    <p class="post-tags">
                        <?php echo $tag->name;?>
                    </p>
                <?php }?>
            </strong>
            <div class="clear"></div>
            <?php 
            if( is_user_logged_in() ) {
                $uid = get_current_user_id();
                $userdata = get_userdata( $uid );
                if( in_array( 'administrator' , $userdata->roles ) ){
                    edit_post_link( 'Edit', '', '', $post->ID, '' ); 
                }
            }
            ?>
            <?php
        }
    }
    ?>

    Thanks!

    Thread Starter Tanath Fedo

    (@tanath-fedo)

    Thank you!

    I changed back the tags code, it wasn’t displaying as links and I couldn’t find how to edit what you did.

    It also needed ‘category_name’ rather than just ‘category’, and 1, not -1 (-1 made it repeat the post over and over down the page forever)

    As well as echo apply_filters('the_content', $post->post_content); not just echo $post->post_content;

    Working good now, all updated to the latest greatest, fastest loading code, I hope!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Should I replace query_posts on my index page?’ is closed to new replies.