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 have written a neat responsive slider for my Wordpress featured articles (feel free to use it):

<?php 
    $responsive = 'on' != get_option('henrik_responsive_layout') ? false : true;
    $featured_auto_class = '';
    if ( 'on' == get_option('henrik_slider_auto') ) $featured_auto_class .= ' et_slider_auto et_slider_speed_' . get_option('henrik_slider_autospeed');
?>
<div id="featured" class="<?php if ( $responsive ) echo 'flexslider' . $featured_auto_class; else echo 'et_cycle'; ?>">
    <a id="left-arrow" href="#"><?php esc_html_e('Previous','henrik'); ?></a>
    <a id="right-arrow" href="#"><?php esc_html_e('Next','henrik'); ?></a>

<?php if ( $responsive ) { ?>
    <ul class="slides">
<?php } else { ?>
    <div id="slides">
<?php } ?>
        <?php global $ids;
        $ids = array();
        $arr = array();
        $i=0;

        $featured_cat = get_option('henrik_feat_cat'); 
        $featured_num = (int) get_option('henrik_featured_num'); 

        if (get_option('henrik_use_pages') == 'false') query_posts("showposts=$featured_num&cat=".get_cat_ID($featured_cat));
        else {
            global $pages_number;

            if (get_option('henrik_feat_pages') <> '') $featured_num = count(get_option('henrik_feat_pages'));
            else $featured_num = $pages_number;

            query_posts(array
                            ('post_type' => 'page',
                            'orderby' => 'menu_order',
                            'order' => 'ASC',
                            'post__in' => (array) get_option('henrik_feat_pages'),
                            'showposts' => (int) $featured_num
                        ));
        } ?>
        <?php if (have_posts()) : while (have_posts()) : the_post();
        global $post; ?>
        <?php if ( $responsive ) { ?>
            <li class="slide">
        <?php } else { ?>
            <div class="slide">
        <?php } ?>
                <?php
                $width = $responsive ? 960 : 958;
                $height = 340;
                $small_width = 95;
                $small_height = 54;
                $titletext = get_the_title();

                $thumbnail = get_thumbnail($width,$height,'',$titletext,$titletext,false,'Featured');

                $arr[$i]['thumbnail'] = get_thumbnail($small_width,$small_height,'',$titletext,$titletext,false,'Small');
                $arr[$i]['titletext'] = $titletext;

                $thumb = $thumbnail["thumb"];
                print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, ''); ?>
                <div class="featured-top-shadow"></div>
                <div class="featured-bottom-shadow"></div>  
                <div class="featured-description">
                    <div class="feat_desc">
                        <p class="meta-info"><?php esc_html_e('Posted','henrik'); ?> <?php esc_html_e('by','henrik'); ?> <?php the_author_posts_link(); ?> <?php esc_html_e('on','henrik'); ?> <?php the_time(esc_attr(get_option('henrik_date_format'))) ?></p>
                        <h2 class="featured-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                        <p><?php truncate_post(410); ?></p>
                    </div>
                    <a href="<?php the_permalink(); ?>" class="readmore"><?php esc_html_e('Read More', 'henrik'); ?></a>
                </div> <!-- end .description -->
        <?php if ( $responsive ) { ?>
            </li> <!-- end .slide -->
        <?php } else { ?>
            </div> <!-- end .slide -->
        <?php } ?>
        <?php $ids[] = $post->ID; $i++; endwhile; endif; wp_reset_query(); ?>
<?php if ( $responsive ) { ?>
    </ul> <!-- end .slides -->
<?php } else { ?>
    </div> <!-- end #slides -->
<?php } ?>
</div> <!-- end #featured -->

<div id="controllers" class="clearfix">
    <ul>
        <?php for ($i = 0; $i < $featured_num; $i++) { ?>
            <li>
                <div class="controller">
                    <a href="#"<?php if ( $i == 0 ) echo ' class="active"'; ?>>
                        <?php print_thumbnail( $arr[$i]['thumbnail']['thumb'], $arr[$i]['thumbnail']["use_timthumb"], $arr[$i]['titletext'], $small_width, $small_height ); ?>
                        <span class="overlay"></span>
                    </a>
                </div>  
            </li>
        <?php } ?>
    </ul>
    <div id="active_item"></div>
</div> <!-- end #controllers -->

It selects a custom category and returns a custom amount of posts. The problem is that it will display the posts in the order of which they were created. I want to choose my own order, so that I can select which posts displays first and so on.

So, is it possible to select which order the returned posts are displayed? Maybe by targeting specific IDs?

Ideally, I would like to set a custom ID (directly in the code) for the first post, and then have the rest display in the returned order as it is. Is that possible?

If you choose to reply, please be detailed with code example, thank you for reading.

share|improve this question

1 Answer 1

In order to accomplish what you desire, you'll have to fetch the posts ahead of time. There is no way to specify in SQL an arbitrary ordering given an ID list.

query_posts() creates another main query, throwing out the main query. Since you're doing this anyway, you can use get_posts() to return a list of posts instead.

$posts = get_posts( array(
    'post_type' => 'page',
    'orderby' => 'menu_order',
    'order' => 'ASC',
    'post__in' => (array) get_option('henrik_feat_pages'),
    'showposts' => (int) $featured_num
) );

Then,

$posts_by_id = array(); 
foreach( $posts as $post ) $posts_by_id[$post->ID] = $post;

Now you have an array of post objects indexed by ID, and you can iterate through your ordered list and select the posts from that list:

global $post;
foreach( (array) get_option( 'henrik_feat_pages' ) as $post_id ) {
    $post = $posts_by_id[$post_id];
    setup_postdata( $post )

    // ... Template code as if you were in the loop.
}

Update

The loop above replaces the main loop. So instead of writing

<?php if (have_posts()) : while (have_posts()) : the_post();
    <?php /** Code in loop **/ ?>
<?php endwhile; endif; ?>

To initiate the loop, we use:

global $post;
foreach( (array) get_option( 'henrik_feat_pages' ) as $post_id ) {
    $post = $posts_by_id[$post_id];
    setup_postdata( $post );

    /** Code in loop **/
}

This is equivalent code that uses a slightly different syntax. $post must be globalized and set to the correct post object on each iteration, and setup_postdata() populates all of the related post variables with the information found in $post.

This code assumes that the option henrik_feat_pages is an ordered list of post IDs.

Update 2

How to show a specific post with a known post ID, $first_post_id first:

// Create ordered array of posts to show, with $first_post_id first
$posts_to_show = array_merge( (array) $first_post_id, (array) get_option( 'henrik_feat_pages' ) );

// Get the pages whose IDs are in $posts_to_show
$posts = get_posts( array(
    'post_type' => 'page',
    'post__in' => $posts_to_show
) );

// Create ordered post array:
$posts_by_id = array(); 
foreach( $posts as $post ) $posts_by_id[$post->ID] = $post;

// Output loop:
global $post;
foreach( $posts_to_show as $post_id ) {
    $post = $posts_by_id[$post_id];
    setup_postdata( $post )

    // ... Template code as if you were in the loop.
}

Note that I simplified the query in get_posts() so that only the post_type and post__in arguments are supplied. There is no need to specify ordering, since the posts are being ordered in code.

share|improve this answer
    
Excellent reply. However can you please be detailed in the last part so that I can try this myself and see if it works. How do I do the selection part? –  Amanda Bynes Sep 3 '12 at 21:45
    
Thank you for the update. My confusing is still around exactly how I make the selection of posts? Will this be directly in the code? My apologies but after fiddling with my code for a week and being stuck, my mind is empty. –  Amanda Bynes Sep 3 '12 at 22:07
    
The selection of the posts is the same, except you're using get_posts() instead of query_posts(). I demonstrated this in my first code example. –  Bendoh Sep 3 '12 at 22:19
    
If I understand this correctly, my options to order the list is now limited to the Order/Orderby parameters? codex.wordpress.org/Class_Reference/… –  Amanda Bynes Sep 3 '12 at 22:23
    
One final question if you could be kind. Is it possible in my code to make the first that is displayed to be a custom one? For instance, can I add an ID of a specific post directly in my code to make that post appear first, followed by the other ones that are returned on the original query? This would solve my question. –  Amanda Bynes Sep 3 '12 at 22:30

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.