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'm looking for some thoughts, ideas, and/or direction on what is the best way to accomplish this:

Basically I want to have 1 "featured" post in a differently styled div, and then display the next 3 below it.

I am using custom post types, so the built-in Sticky Posts feature won't work, and I'd rather not use a plugin...

Here is a screenshot of what I'm trying to accomplish:
enter image description here

I haven't done this in WordPress before so I'm not really sure where to begin. I assume I should set up a category or a tag called "featured" to trigger which posts will display at the top. But then how do I get it not to display in the regular feed?
I'm guessing I can probably disallow the "featured" tag or category, but that work-flow seems cumbersome (for the client to have to uncheck past featured posts for them to show back up in the regular feed then).

share|improve this question
    
Is the featured post an arbitrarily chosen post? Or is it the first post displayed in a chronological order (by date)? –  userabuser Nov 13 '12 at 1:20
    
In chronological order, by date. –  ByteMyPixel Nov 13 '12 at 20:15

1 Answer 1

up vote 0 down vote accepted

One way to achieve this is to establish two queries/loops, one that handles the featured post and the other which handles all other posts (except the featured post).

How you distinguish what IS and what is NOT to be a featured post could be handled by a special placeholder category or by the use of a custom field, the latter of which being more appropriate than cluttering up your category listings with placeholder categories that have no real value.

So lets assume that for posts that you want to be featured that you assign a custom field with a meta_key of featured_post and a meta_value of 1.

In your template file you would then do something similar to this;

$args = array(
   'posts_per_page' => 1,
   'post_type' => 'your_post_type',
   'order' => 'DESC',  
   'meta_query' => array( 
      array(
         'key' => 'featured_post', 
         'value' => '1',
      )
   )
);


$first_query = new WP_Query( $args );
if ( $first_query->have_posts() ):
   while( $first_query->have_posts() ) : $first_query->the_post();

        echo '<div class="featured_post">';

            //your desired output here....      

        echo '</div>';

   endwhile;
endif;
wp_reset_postdata();


$args = array(
   'posts_per_page' => 9,
   'post_type' => 'your_post_type',
   'meta_query' => array( 
      array(
         'key' => 'featured_post', 
         'value' => '1',
         'compare' => 'NOT LIKE' //or NOT or != should suffice
      )
   )
);


$second_query = new WP_Query( $args );
if ( $second_query->have_posts() ):
   while( $second_query->have_posts() ) : $second_query->the_post();

        echo '<div class="regular_posts">';

            //your desired output here....      

        echo '</div>';

   endwhile;
endif;
wp_reset_postdata();

Some questions you might have...

Q: What happens in the case where I assign (over time) more than one post as featured using the featured_post key and value 1 and forget to edit previously featured posts?

In that case, the parameters; 'order' => 'DESC' shown in the first query that control the display of your featured post will show the most recent to be assigned the post meta key/value pairing based upon DATE in chronological order from the most recent post date to the eldest.

Q: ...but what happens if I want to show a featured post from a random date and where a post with a more recent date is already assigned the post meta key/value pair?

In this case you will need to remember to remove the post meta key/value pair from any featured post that is newer by way of date so that an older post with the post meta key/value pair can take priority place.

Q: Are there any other ways to do this?

Sure are! Depending upon your requirements there are other ways we could assign featured posts for the first query that make managing changes potentially easier and this may include the use of custom constructed meta boxes that show you currently assigned featured post of this post type and provide a means to alter the featured post on a global fashion even if within the post edit screen. If your requirements exceed the above solution, we can look at this more closely.


Some helpful Codex resources:

share|improve this answer
1  
Thank you userabuser! This is fantastic, and thank you so much for taking the time to explain the details. This is exactly what I needed!!! :) –  ByteMyPixel Nov 13 '12 at 20:14
    
just an fyi - there was a comma missing in the first loop: 'order' => 'DESC', –  ByteMyPixel Nov 29 '12 at 23:39
    
Indeed there is.. I threw that in there to test you ;) haha.. not. Thanks for pointing that out, I'll update that now. –  userabuser Nov 30 '12 at 1:31

Your Answer

 
discard

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

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