So I have Custom post type that occasionally will have posts marked as 'featured' Client would like to use the 'sticky' feature of WordPress to pick which posts from the CPT are classed as featured and would like the layout of the page to show featured (a) and normal posts (b) in a different fashion.

I would like to work out the best way of creating a loop that can stagger the two sets of posts in a uniform way, that can behave with pagination if possible.

I have boiled it down to (perhaps rightly or wrongly):

  1. Created two arrays of posts - one for just stickies, one for just normal posts
  2. Somehow merge the two arrays into one in a set pattern:

a1, a2, b1, b2, a3, b3, b4, a4, b5, b6 etc etc.

$sticky = get_option( 'sticky_posts' );

$args_ordinary = array(
    'post__not_in' => $sticky,
    'post_type' => 'research-articles',
    'posts_per_page' => 10,
    'order' => 'ASC',

$args_sticky = array(
    'post__in'  => $sticky,
    'posts_per_page' => 3,
    'order' => 'ASC',
    'post_type' => 'research-articles',
$ordinaryQuery = new WP_Query($args_ordinary);
$ordinaryPosts = $ordinaryQuery->posts;

$stickyQuery = new WP_Query($args_sticky);
$stickyPosts = $stickyQuery->posts;

$result = $stickyPosts + $ordinaryPosts;
$result = array_replace(array_fill_keys(array(0,1,3,4,2,5,6,7), null), $result);
foreach ($result as $post){ ?>
    <p><?php echo(is_sticky() ? '(sticky) ' : '') ?><?php echo get_the_title() ?></p>
<?php }

My question is really, is the sticky functionality the best way to go about this? and what suggestions for achieving this kind of loop would you guys have (either by using a merged array or perhaps something better)?

I would prefer not to use any plugins to achieve this, as the client has requested not to.

Any feedback is greatly appreciated!

  • Hi Roxy, and what problem do you have with current solution based on sticky posts? Could you show some code, pls? – Krzysiek Dróżdż Jan 14 '19 at 21:24
  • Hi Krzysiek, I've added some code for your consideration. – Roxy Jan 15 '19 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.