Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Christopher Paccione
Christopher Paccione
14,831 Points

Ordering custom post types

Hello,

Is there a way to order custom post types on a page? Right now the first post type entered will eventually display as the last one in the order as they are filled on the page. Is there a way to make the first custom post type entered stay as the first listed on the page?

Does that makes sense?

Here is my code.

<?php
/*
Template Name: Callout
*/
?>

<?php get_header(); ?>

            <div class="row">           
                <div class="large-12 columns">

                    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                        <?php the_content(); ?>

                    <?php endwhile; endif; ?>

                </div>
            </div>

            <?php

                $args = array('post_type' => 'callout');
                $query = new WP_Query($args);

            ?>

            <div class="row text-center callout">

                <?php if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

                    <div class="medium-3 large-3 column">   

                        <h3><?php the_field('callout_heading'); ?></h3>     

                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('large'); ?></a>

                        <p><?php the_field('description'); ?></p>   

                    </div>

                <?php endwhile; endif; wp_reset_postdata(); ?>

            </div>



<?php get_footer(); ?>

6 Answers

Andrew Shook
Andrew Shook
31,709 Points

Ok, I'm confused. You said:

"Right now the first post type entered will eventually display as the last one in the order as they are filled on the page."

That lead me to believe you were talking about order multiple post types on one page. However, now I believe what you meant 'post' and not 'post type'. If you want is to get all posts where post_type = 'Callout' and order them from oldest (top of the page) to newest (bottom of the page) then this tweak to your code should fix you up:

<?php
/*
Template Name: Callout
*/
?>

<?php get_header(); ?>

            <div class="row">           
                <div class="large-12 columns">

                    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                        <?php the_content(); ?>

                    <?php endwhile; endif; ?>

                </div>
            </div>

            <?php

                $args = array('post_type' => 'callout', 'order' => 'ASC', 'orderby' => 'date');
                $query = new WP_Query($args);

            ?>

            <div class="row text-center callout">

                <?php if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

                    <div class="medium-3 large-3 column">   

                        <h3><?php the_field('callout_heading'); ?></h3>     

                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('large'); ?></a>

                        <p><?php the_field('description'); ?></p>   

                    </div>

                <?php endwhile; endif; wp_reset_postdata(); ?>

            </div>



<?php get_footer(); ?>
Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Check out the options for ordering and sorting for WP_Query, this will let you order and sort by other options :)

Andrew Shook
Andrew Shook
31,709 Points

How are they entering/adding custom post types to the query?

Christopher Paccione
Christopher Paccione
14,831 Points

Through the WordPress CMS. I've installed Custom Fields and Custom Post Types UI.

Andrew Shook
Andrew Shook
31,709 Points

I understand that it's WordPress. I was wondering how the multiple post types are being added? Are you hard coding the post types in or are the end users adding them via an input field?

Christopher Paccione
Christopher Paccione
14,831 Points

Thanks Andrew. I wasn't attempting to be snarky.

I'm doing this as test, and I'm adding the images and text through the input fields of the Custom Post Type UI called "Callout".

I'm pulling in the featured image, text field and text area.

I don't believe I'm hardcoding anything in this way, but I could be wrong. I'm attempting to repeat what I learned from Master Zac.

Christopher Paccione
Christopher Paccione
14,831 Points

Excellent, thanks to the both of you, that definitely answers my question! Sorry for the confusion.