1

I have a static front page that lists out news. Right now, it ignores the stickiness of a post, and justs lists it. I want it to work the same way as my blog posts page does. I want it to list out all news, with the stickies on top.

I have googled a bit, but not found anything that does this just with a single query. I can use the 'post__in' => get_option( 'sticky_posts' ) on the args object when making the query, but that only returns the stickies, not the rest of the news.

Do I have to make 2 queries? The functionality I want is right there on the blog posts static page, but not on my static front page.

  • It's built to work on the blog front page and elsewhere requires some effort. Search this site for "sticky" too and update your question with your effort as you'll probably get more offers of help if you can show that. I'd be glad to see a good answer myself, as making 2 queries would upset any pagination, if you want pagination. – Andy Macaulay-Brook Jul 26 '16 at 11:20
  • Well, I haven't really found that much usefull in terms of effort, other than the post__in parameter. – ptf Jul 26 '16 at 11:52
0

I'm guessing that in get_option( 'sticky_posts' ) is array of posts that are sticked? I suggest changing this method to post meta values.

You could mark news as sticky by adding post meta to news, for example add_post_meta($post_id, 'sticky_post', 1); and then you should order posts by this meta:

$args = array(
    'post_type' => 'news',
    'meta_query' => array(
        'relation' => 'OR',
        array( // get posts with sticky_key value = 1
            'key' => 'sticky_post',
            'value' => '1', 
        ),
        array( // get posts with sticky_key value != 1, so this is rest of posts
            'key' => 'sticky_post',
            'value' => '1',
            'compare' => '!='
        )
    ),
    'orderby' => array(
        'meta_value_num' => 'DESC', // first order by sticky_post value, so you could create levels of "stickness"
        'post_date' => 'DESC'       // then normal sort
    )
);

$query = new WP_Query($args);

Your Answer

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

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