Posts Slider - Keep Sticky First in Order

kenny's Avatar

kenny

31 May, 2018 02:41 AM

Hi,
I have a Posts slider (ID = 2) that just pulls all posts in any category and displays them in order by date. This works fine.

But we want a specific post/slide to ALWAYS be first in the order. We know we can tell the client to just keep updating the date on the 'intro' slide to keep it first, but it's a hassle and they may forget.

  • If we tag just one post as 'Sticky', any way to keep it first and then retain the rest of the order as normal???

I tried to piece something together from your 'Modifying order of Posts...' topic and code I found for displaying a sticky post first, but I'm a complete novice with PHP and it's not doing anything. Here's the code I came up with if you can see if I'm at least on the right track and/or missing something obvious in regards to syntax.

function add_sticky_posts_to_slider($slides, $options, $type) {
    $options = '2';
    $sticky = get_option( 'sticky_posts' );
    rsort( $sticky );
    $args = array(
        'post__in' => $sticky,
        'caller_get_posts' => 1,        
        'showposts' => -1
    );
    $query = new WP_Query($args);
    
    $additiona_slides = (array)$query->posts;
    if( is_array($additiona_slides) ) {
        $slides = array_merge($slides, $additiona_slides);
    }
    return $slides; // slides is array of WP Post objects that will be displayed in slider
}
add_filter('new_rs_slides_filter', 'add_sticky_posts_to_slider', 10, 3);

Thanks for any direction here. If there's an easier way to do this, whether by specifying a post ID, using some meta info, etc, we're totally open. Doesn't have to use Sticky feature, just thought that would be obvious.

  1. Support Staff 1 Posted by Dmitry Semenov on 31 May, 2018 03:57 AM

    Dmitry Semenov's Avatar

    Hello,

    Here is an example:

    function add_additional_posts_to_slider($slides, $options, $type) {
    
    
        $additional_post_ids = array( 1, 2, 3 );
    
        $args = array(
            'post_type' => 'post',
            'post__in' => $additional_post_ids,
        );
        $query = new WP_Query($args);
        
        $additiona_slides = (array)$query->posts;
        if( is_array($additiona_slides) ) {
            $slides = array_merge($additiona_slides, $slides);
        }
    
        return $slides;
    }
    add_filter('new_rs_slides_filter', 'add_additional_posts_to_slider', 10, 3);
    

    Replace 1, 2, 3 with IDs of posts that you wish to display at first.

  2. 2 Posted by kenny on 31 May, 2018 06:55 PM

    kenny's Avatar

    Hi Dmitry,
    Thanks for the quick reply here! Much appreciated.

    This is close to working properly. Wonder how I would modify to:

    A) Apply it to only the slider with an ID of '2' (our Home page slider). Currently it looks like it's messing up a gallery slider in another location.

    B) Exclude the post specified by ID in the normal, secondary query. Currently, the post I select by ID shows up first, but then shows up a second time again in the natural date order.

    Thanks! We'll have another license or two coming your way on a couple upcoming sites.

  3. Support Staff 3 Posted by Dmitry Semenov on 31 May, 2018 08:10 PM

    Dmitry Semenov's Avatar

    Here is how to apply code only to slider with ID 2:

    function use_custom_query_to_rs($slides, $options, $type) {
       if ( !isset($options['id']) || (int)$options['id'] !== 2 ) {
         return $slides;
       }
    
        $args = array(
            'post_type' => 'post'
        );
    
        $query = new WP_Query($args);
    
        return (array)$query->posts;
    }
    add_filter('new_rs_slides_filter', 'use_custom_query_to_rs', 10, 3);
    

    Customizations for complex post query are unfortunately out of support scope. You can check WP Query arguments at https://gist.github.com/luetkemj/2023628 The easiest way would probably to run two separate queries and marge their post arrays.

  4. 4 Posted by kenny on 31 May, 2018 10:41 PM

    kenny's Avatar

    Hi Dmitry,
    Thanks for the response. Totally understand. I'll keep trying to learn PHP as I can, but being able to master it and spot best practices or slight syntax errors can take a fair amount of time (especially while trying to get other work done ; )

    So I tried adding your second bit of code, replacing what I had there. I didn't see the option to choose an 'additional post id' to include first, so didn't expect it to do much of anything. Assume that was just an example I'd have to piece together with the first bit of code, correct?!?

    But for some reason, the sticky posts option now seems to WORK!! I'm not sure why, but it 'appears' to work perfectly now. Setting a post as 'Sticky' causes it to show up first, then the others follow in date order. And no duplicate posts, nor does it mess up my other Slider!

    So thanks for the help here! Hopefully it can help someone else trying to get Sticky posts to work like I was. Was this the intended result (your comments led me to believe it would only help with the targeting a particular slider issue).

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac