0

I have a really strange issue for a site I'm updating...

All I want to do, is have it so that the posts come in descending order instead of random. I have the following code, but when you change it from "rand" to "desc" it just defaults to pull in the posts in "asc" order. I have no idea why. In addition, if you change "orderby" to "order" it just defaults to ascending order... I've done a search through the theme to see where that's happening but there's no place at all where that's listed... can someone please steer me in the right direction?

 <?php

 $args= array(
 'post_type' => 'waiting-families',
'orderby' => 'rand'

);

 // The Query
 $the_query = new WP_Query( $args );



 // The Loop
 if (have_posts() ):

while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">

    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <?php if ( has_post_thumbnail()) { ?>
            <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($id,      'thumbnail', array('class' => 'alignleft','style' => 'width: 150px;height: 150px;padding: 3px;     background: #f0f0f0; border: 1px solid #65C8C6;')); ?></a>
        <?php } ?>
        <?php my_excerpt(55); ?>

        <p class="postmetadata">
            <?php edit_post_link('Edit', '', ' | '); ?>
        </p>
   </div>

  <?php endwhile; 
else: echo '<p style="text-align: center; font-size:20px; font-weight: 700; color:         #65c8c6;">More Families to Come.</p>';
 endif;
   ?>


   <?php // Reset Post Data
  wp_reset_postdata(); ?>
1
  • It looks correct, what happens if you change your IF statement to if($the_query->have_posts()) : ? Hard to tell what the problem is on this code, you'll mostly end up with guesses.
    – Howdy_McGee
    Aug 26, 2014 at 21:52

1 Answer 1

1

orderby does not accept the values ASC or DESC. These are acceptable values for the parameter order. Changing 'orderby' => 'rand' to 'order' => 'ASC' should be sufficient and should by rule work

As already pointed out,

if (have_posts() ):

should be

if ($the_query->have_posts() ): 

Apart from that little discrepancy, you code have to work

It case this does not solve your problem, look in your functions.php for any instance of pre_get_posts. The reason for my thinking is, pre_get_posts does not only alter the main query, but also all instances of WP_Query. You have to include the conditional tag is_main_query() to target the main query only, otherwise pre_get_posts will interfere with instances of WP_Query

For additional resources:

1
  • Sorry for the tardy reply, yes this fixed that issue! Jun 16, 2016 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.