Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using query_post to randomize the posts in a custom post type. However this causes the posts to be loaded reloaded when i load more posts in.

Below is my code:

<?php query_posts($query_string . '&orderby=rand'); ?>
        <?php if (have_posts()) : ?>
          <div id="artistWrap">
            <?php while (have_posts()) : the_post(); ?>
                <article id="post-<?php the_ID(); ?>" class="col-lg-4 work">
                    <div class="innerWrapper">
                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                            <?php the_post_thumbnail('full', array('class' => 'img-responsive'));   ?>
                        </a>

                        <h2>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                                <?php the_title(); ?>
                            </a>
                        </h2>                           
                    </div>
                </article><!-- #post-## -->

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

If i remove query_posts and just use the have_posts it doesn't load the posts in twice, but doesn't randomize it.


Update:

I have tried changing it to use wp_query and I still get the same problem with it loading in duplicate posts.

<?php $loop = new WP_Query( array( orderby => 'rand', 'post_type' => 'artist' ) ); ?>

        <?php if (have_posts()) : ?>
          <div id="artistWrap">
            <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
                <article id="post-<?php the_ID(); ?>" class="col-lg-6 work">
                    <div class="innerWrapper">
                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                            <?php the_post_thumbnail('full', array('class' => 'img-responsive'));   ?>
                        </a>

                        <h2>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                                <?php the_title(); ?>
                            </a>
                        </h2>                           
                    </div>
                </article><!-- #post-## -->

            <?php endwhile; ?> 
          </div>
        <?php endif; wp_reset_query(); ?>
share|improve this question
    
query_post() shouldn't be used overall, see the tag for questions on the topic. –  Rarst Jun 19 at 19:50
    
I have updated it to use wp_query and I still get the same problem. –  user2648610 Jun 19 at 20:09
    
if you want to modify the main query, you should be using pre_get_posts, not creating new queries in the template. –  Milo Jun 19 at 20:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.