How do you set up a custom query to only show sticky posts in a slider? I have your hero posts slider in place just need to get it showing the right posts.
Tried this but didn’t work… $sticky = get_option( 'sticky_posts' );
How do you set up a custom query to only show sticky posts in a slider? I have your hero posts slider in place just need to get it showing the right posts.
Tried this but didn’t work… $sticky = get_option( 'sticky_posts' );
Hey Daniel,
Thanks for writing in! You are using the incorrect query string. You should be using:
post_type="post"&posts_per_page=-1&post__in=get_option( 'sticky_posts' )
For reference, please check this out:
Best Regards.
Hello Daniel,
In case if you want to exclude the sticky from the post list you can use this query string the exclude the sticky posts.
post_type=post&post_status=publish&ignore_sticky_posts=1
Hope it helps
Thanks
So I’m using
post_type="post"&posts_per_page=-1&post__in=get_option( 'sticky_posts' )
in my slider
and
post_type=post&post_status=publish&ignore_sticky_posts=1
in my bloglist but it’s not excluding the sticky post which is Christmas 2020:
Hello Daniel,
In order to help you with your concerns, we need to check your settings so I would request you please send your details in a secure note. Please provide the following details
You can find the Secure Note button at the bottom of your posts
Thanks
Details are in the secure note now.
Hello Daniel,
Adding the $sticky = get_option( 'sticky_posts' )
in your query complicates it and it will not return post objects properly. You must create your own Custom Looper provider to be able to display the sticky posts or exclude it. Perhaps, this thread can help you:
Best Regards.
Hello @ruenel,
Hm! I just want to be able to feature sticky posts in a slider and exclude them from the rest of the list below. Why doesn’t Wordpress allow us to accomplish this more easily
Is there any other way to do this?
Ok, if it is not already obvious to you then… I have no idea what I am doing. Very much learning as I go. But, I thought I’d add what I have done before you respond to my post above.
I found this bit of code after a search around, placed it in my functions.php file. Added wpc_ignore_sticky
to the bloglist looper provider. It appears to work in that it’s now hiding the climate post. However the slider has disappeared despite having post_type="post"&posts_per_page=-1&post__in=get_option( 'sticky_posts' )
Seems I have a lot of learning to do
/**
* Snippet Name: Ignore sticky posts from the main query
* Snippet URL: https://wpcustoms.net/snippets/ignore-sticky-posts-main-query/
*/
function wpc_ignore_sticky($query)
{
if (is_home() && $query->is_main_query())
$query->set('ignore_sticky_posts', true);
$query->set('post__not_in', get_option('sticky_posts'));
}
add_action('pre_get_posts', 'wpc_ignore_sticky');
Hello Daniel,
Your code affects both of the Loopers. It is best that you create two separate custom looper functions that;
1.) will return only the sticky posts and
2.) will exclude sticky posts
Perhaps these threads may help you:
For the documentation of the custom Looper provider, you can check this out:
Hm! Ok, I have the following in functions.php
add_filter( 'cs_looper_custom_ignore_sticky', function( $result, $args ) {
$args = array(
'post_type' => 'posts',
'posts_per_page' => -1,
'order' => 'ASC',
'post__not_in' => get_option( 'sticky_posts' )
);
return get_posts($args);
}, 10, 2);
add_filter( 'cs_looper_custom_only_sticky', function( $result, $args ) {
$args = array(
'post_type' => 'posts',
'posts_per_page' => -1,
'order' => 'ASC',
'post__in' => get_option( 'sticky_posts' )
);
return get_posts($args);
}, 10, 2);
then in the looper provider section of the slider
I have cs_looper_custom_only_sticky
with nothing in Params and offset is turned off (not sure if that is correct–please confirm).
Then in the bloglist
section I have post_type=post&post_status=publish&cs_looper_custom_ignore_sticky
with nothing in params and offset turned off (again not sure if this is correct).
But in the blog list I have the climate post showing (which is sticky) and no other posts.
EDIT: I’d also like to display multiple sticky posts in that slider…
I’d really appreciate some help.
Hello Daniel,
You can insert [{}]
in the params. Your consumer should be generated right away. And by the way, you will have to edit your code and use:
$stickies = get_option( 'sticky_posts' );
And then use the variable for the post__in
or post_not_in
in your code.
Please note that custom coding is outside the scope of our support. Issues that might arise from the use of custom code and further enhancements should be directed to a third party developer.
Best Regards.
I know this is out of scope, sorry to be a pain.
Where would I add $stickies = get_option( 'sticky_posts' );
??
With a little help I’ve done:
add_filter( 'cs_looper_custom_ignore_sticky', function( $result, $args ) {
$stickies = get_option( 'sticky_posts' );
$args = array(
'post_type' => 'posts',
'posts_per_page' => -1,
'order' => 'ASC',
'post__not_in' => $stickies
);
return get_posts($args);
}, 10, 2);
add_filter( 'cs_looper_custom_only_sticky', function( $result, $args ) {
$stickies = get_option( 'sticky_posts' );
$args = array(
'post_type' => 'posts',
'posts_per_page' => -1,
'order' => 'ASC',
'post__in' => $stickies
);
return get_posts($args);
}, 10, 2);
However, I’m still getting the same results in that I’m only getting sticky posts.
Hello Daniel,
Please use this modified code instead:
// Return posts excluding the sticky posts
//
add_filter( 'cs_looper_custom_get_posts_no_sticky', function( $result, $args, $element ) {
$sticky = get_option( 'sticky_posts' );
$query = array(
'posts_per_page' => -1,
'post__not_in' => $sticky
);
return get_posts(array_merge( $args, $query) );
}, 10, 3);
// Return only the sticky posts
//
add_filter( 'cs_looper_custom_get_posts_only_sticky', function( $result, $args, $element ) {
$stickies = get_option( 'sticky_posts' );
$query = array(
'post__in' => $stickies,
'posts_per_page' => -1,
);
return get_posts(array_merge( $args, $query) );
}, 10, 3);
Kindly let us know how it goes.
Ok, so to the untrained eye that looks like it could work. I have set up cs_looper_custom_get_posts_no_sticky
in bloglist grid and cs_looper_custom_get_posts_only_sticky
in slider but still getting sticky posts in both sections and no other, on sticky posts.
I am starting to wonder if the problem could be the dynamic content on the blog page
Hello Daniel,
With Custom Looper, you only need to insert get_posts_no_sticky
and get_posts_only_sticky
. Not the whole function name.
Best Regards.
Great stuff! About the slider, if I have more than one sticky post the slider should go through the cycle but it’s not it is just showing the first sticky. Is this something to do with dynamic content?
Hello Daniel,
You forgot to enable the Looper Consumer that is why it is only displaying 1 slide. Please click the Slide element and make it as the Looper Consumer. The number of slides will represent the number of Sticky Posts.
Cheers.