Using categories & “stickyness” together

Just add 'post__in' => get_option('sticky_posts') to your query, to confine your query to only sticky posts. So,

$getHighlights = array(
    'numberposts' => 7,
    'post_type' => array('post','Event'),
    'post__in' => get_option('sticky_posts'),
    'category_name' => 'Highlights'
);

should work for you.

Edit: This is how you can merge two arrays to get the sticky posts at the top of your query:

$getHighlights_sticky = get_posts( array(
        'numberposts' => 7,
        'post_type' => array('post','Event'),
        'post__in' => get_option('sticky_posts'),//it should be post__in but not posts__in
        'category_name' => 'Highlights'
    ));
$getHighlights_other = get_posts( array(
        'numberposts' => 7 - count( $getHighlights_sticky ),
        'post_type' => array('post','Event'),
        'post__not_in' => get_option('sticky_posts'),//it should be post__not_in but not posts__not_in
        'category_name' => 'Highlights'
    ));
foreach ( array_merge( $getHighlights_sticky, $getHighlights_other ) as $post ) {
    setup_postdata( $post );

    // your display loop

}

This will show 7 posts, with the stickies at the top (of course, this is assuming you don’t have more than 7 stickies, otherwise it’ll be all messed up…) (edited to use numberposts as per OP’s comment below…)

Leave a Comment