catdotgif
catdotgif

Reputation: 1775

wordpress custom post type cannot change order of display

I've created a custom post type to do hand-crafted excerpts from my blog on my portfolio site. I've got a content window, a link, and a featured image for the post type, which I have called blog.

The issue is that, no matter what I try, the posts are displayed oldest to newest, whereas I would like to display the newest first. Here's the query_posts() call:

<?php query_posts( 'post_type=blog&order=ASC'); ?>

But I've also tried more elaborate queries such as:

<?php query_posts(array('post_type' => 'blog', 'orderby'=>'date','order'=>'ASC')); ?>

My complete template file looks like: ` ">

    <div class="sliderContent">
        <!--first loop-->
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
        <?php the_content(__('Read More &raquo;', THEMENAME)); ?>
        <?php endwhile; else: ?>
        <p><?php _e('Nothing found.', THEMENAME); ?></p>
        <?php endif; ?>

        <!--second loop, displays custom post type-->
        <?php query_posts(array('post_type' => 'blog', 'orderby'=>'date','order'=>'ASC') ); ?>
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <div class="contenttile">
            <p><a href="<?php echo get_post_meta($post->ID, 'Permalink', true); ?>"><?php the_post_thumbnail('medium'); ?></a></p>
            <h2><?php the_title(); ?></h2>

            <?php the_content(__('Read More &raquo;', THEMENAME)); ?>
        </div>
        <?php endwhile; else: ?>
        <p><?php _e('Nothing found.', THEMENAME); ?></p>
        <?php endif; ?>
    </div>
</div>
<!-- content end -->

<?php } ?>

`

So I'm displaying the content from the page that this template is applied, then I'm displaying my custom post type.

Thanks for the help, I'm stumped!

Upvotes: 0

Views: 4927

Answers (2)

catdotgif
catdotgif

Reputation: 1775

Well, as majorano84 alluded to, after further reading query_posts() is not the right function to use at all (because I guess it makes more work for the server?)

Instead, I used get_posts(), and that displays the posts in the preferred order without any further effort on my part:

        <?php
            $args = array( 'post_type' => 'blog' );
            $lastposts = get_posts( $args );
            foreach($lastposts as $post) : setup_postdata($post); ?>
                <div class="contenttile">
                        <p><a href="<?php echo get_post_meta($post->ID, 'Permalink', true); ?>"><?php the_post_thumbnail('medium'); ?></a></p>
                        <h2><?php the_title(); ?></h2>
                        <?php the_content(); ?>
                </div>
        <?php endforeach; ?>

`

So I'm going to call that the answer, because it solves the problem I was having. Thanks!

Upvotes: 0

maiorano84
maiorano84

Reputation: 11951

Your query is in Ascending order. Ascending order IS oldest to newest. You want DESCENDING order if you want newest to oldest. Also, you should avoid using query_posts if at all possible, as it modifies the default Wordpress loop.

Your second query isn't that much more elaborate than the first. The only difference is you're using an array rather than a string to define the query parameters (which an array is arguably the correct way to go about it), and you're setting the orderby parameter.

Lastly, the default order is by date in descending order (newest to oldest) so you theoretically don't even NEED to define order and orderby parameters.

Try this:

    <!--second loop, displays custom post type-->
    <?php
    $args = array('post_type' => 'blog', 'orderby'=>'date','order'=>'DESC');
    /*Consider changing to: $args = array('post_type' => 'blog');*/
    $query = new WP_Query($args);
    if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
    ?>
    <div class="contenttile">
        <p><a href="<?php echo get_post_meta($post->ID, 'Permalink', true); ?>"><?php the_post_thumbnail('medium'); ?></a></p>
        <h2><?php the_title(); ?></h2>

        <?php the_content(__('Read More &raquo;', THEMENAME)); ?>
    </div>
    <?php endwhile; else: ?>
    <p><?php _e('Nothing found.', THEMENAME); ?></p>
    <?php
    endif;
    wp_reset_postdata();
    ?>
</div>

Upvotes: 1

Related Questions