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.
/* Mini-Features */
        ?>
        <div id="sub-featured" class="<?php echo $main_css_class; ?> section">
            <h2 class="section-title"><?php echo stripslashes( $featured_title ); ?></h2>

            <?php 
                query_posts( 'suppress_filters=0&post_type=infobox&order=ASC&posts_per_page=' . $mini_features_number );
                if ( have_posts() ) { $count = 0; while ( have_posts() ) { the_post(); $count++;

                $excerpt = stripslashes( get_post_meta( $post->ID, 'mini_excerpt', true ) ); 
                $button = get_post_meta( $post->ID, 'mini_readmore', true );
                $post_class = 'post block';

                if ( $count % $mini_features_count == 0 ) { $post_class .= ' last'; }
            ?>
                <div <?php post_class( $post_class ); ?>>

                        <a href="<?php echo $button; ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php woo_image( 'key=mini-image&width=300&height=150&class=thumbnail aligncenter&link=img' ); ?></a>

                        <h3 class="title"><a href="<?php echo $button; ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                        <?php if ( $excerpt ) { ?>
                        <div class="entry">
                            <?php echo $excerpt; ?>
                        </div>
                        <?php } ?>

                </div><!-- /.post -->
            <?php
                if ( $count % $mini_features_count == 0 ) { echo '<div class="fix"></div>'; }
                } // End WHILE Loop
            } else {
            ?>
                <div class="post">
                    <p><?php _e( 'Sorry, no posts matched your criteria.', 'woothemes' ); ?></p>
                </div><!-- /.post -->
            <?php } // End IF Statement ?>  
        </div><!-- /#mini-features -->

That's the code, somewhere within that I need to specify that only sticky_posts are displayed - this is where I come unstuck. I've taken a look at the Codex for both query_posts & sticky_posts & I've got to be honest - I'm not sure where I'd start to modify the above... The first thing I did was add the ability (to those custom post types) to include a Sticky Post meta box (thanks to: Sticky Custom Post Types - this worked a treat).

As part of the theme options I can specify how many posts I'd like to display (in my case 3x) but until I modify the above code to only display sticky_posts - I'll end up with 2x sticky_posts (& one other random one). I'm stuck, so any pointers would be greatly appreciated!

share|improve this question
1  
is this the main query for the page, or an additional query? –  Milo Nov 12 '12 at 1:38
    
I'd say it's additional; –  Jason Nov 12 '12 at 2:10
    
The full page code: link –  Jason Nov 12 '12 at 2:13

1 Answer 1

This may get a bit complicated, I would recommend not adding the sticky feature to other post types if you want to query them specifically, and instead using post meta to designate custom types as "sticky". If you use the sticky feature on multiple post types, you'll get unexpected results with the following code, as sticky posts are stored as an array of post IDs independent of the type of post they refer to. If you're not using the sticky feature on other post types, then the following code should work as expected.

First, sticky posts are saved in the option 'sticky_posts', to get a limited number of these IDs, we need to slice the array to grab only the amount we want, and then we pass that as the post__in argument of our query.

Second, sticky posts are added to a query on top of what you specify in the query, so one of the parameters we need to set is to ignore sticky posts, which seems counter intuitive, but we want to explicitly query for these posts ourselves, not have them added onto the query.

Lastly, since this is an additional query, we don't want to use query_posts, we use a new WP_Query instead.

// limit the number to our mini features number
$sticky = array_slice( get_option('sticky_posts'), 0, $mini_features_number );

$mini_features = new WP_Query(array(
    'post_type' => 'infobox',
    'post__in' => $sticky,
    'ignore_sticky_posts' => 1,
    'posts_per_page' => $mini_features_number // possibly redundant, but in case it's larger than default posts_per_page
));

while( $mini_features->have_posts() ):
    $mini_features->the_post();
    // your loop stuff
endwhile;
share|improve this answer
    
Wow, I'd never have had a chance. Thanks so much, I'll read (& no doubt re-read) through this. Thanks again! –  Jason Nov 12 '12 at 2:52

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.