Specific post always first in the loop?

I have a loop that outputs some posts from a custom post type. My question is:

If I have one post with the category “highlighted”, is it possible to make this post always display as number one in the output?

Here is my code:

            <?php
                $args = array(
                    'post_type' => 'offers',
                    'posts_per_page' => 10
                );
                $the_query = new WP_Query( $args );
            ?>
            <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

.......

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

There is probably a slick way to code what you want. I had the same situation once, though, which I solved by having two post queries - the first one displaying the sticky post (in your case the ‘highlghted’ one), and then the second loop for the rest of the posts. Maybe not the most efficient way to do it, but it worked fine. You just have to make sure to reset your query between the two loops.

If you’re using a single query to get all the posts, when you’re looping through the result set you could use an if block (assuming that you’re using a while loop to get the results from the result set) where if the category is highligted use array-unshift() to append the record to the front of the array with the result set.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.