-
AuthorPosts
-
September 9, 2021 at 4:17 pm #1320278
Hello,
I have several post grids on my website, and would like to implement a solution allowing sticky posts to be actually sticki-ed (aka, first in the grid).
There is a solution on these forums, but it doesn’t seem to be working anymore :// sticky posts add_filter('avia_blog_post_query', 'avia_blog_post_query_mod', 10, 2); function avia_blog_post_query_mod($query, $params) { $include = array(); $sticky = get_option( 'sticky_posts' ); $args = array( 'taxonomy' => $params['taxonomy'], 'post__not_in' => $sticky, ); $posts = get_posts( $args ); foreach($posts as $post) { $include[] = $post->ID; } $include = array_merge($sticky, $include); // convert values of the $include from string to int function sti($n) { settype($n, 'int'); return $n ; } $include = array_map("sti", $include); $query['post__in'] = $include; $query['posts_per_page'] = 6; $query['orderby'] = 'post__in'; // sort items based on the post__in value return $query; }
I also found another code, but this one doen’t work if the styckied post is too far down the query (ex, 7th in the query whereas the grid onmy shows 6) :
add_filter('the_posts', 'bump_sticky_posts_to_top'); function bump_sticky_posts_to_top($posts) { foreach($posts as $i => $post) { if(is_sticky($post->ID)) { $stickies[] = $post; unset($posts[$i]); } } if(!empty($stickies)) return array_merge($stickies, $posts); return $posts; }
Is there an updated version of the first code, that would work for several blog grids (there are some on different pages, usually with 3 posts and no pagination, and a big one with pagination).
Thanks !
September 13, 2021 at 7:36 am #1320637Hey JaimBateman,
Thank you for the inquiry.
The same code above should work for sticky posts but for the grid layout, you have to replace avia_blog_post_query with the avia_post_slide_query filter. Let us know if that helps.
Best regards,
IsmaelSeptember 13, 2021 at 2:41 pm #1320731Hey Ismael,
Thanks for that. I’ve tried the code, and something weird happens : I cannot save the function.php file, and when I do, it causes a critical error on the site. When I try to save and it doesn’t, here’s what it’s telling me :
Your php code modifications have been cancelled due to an error on line 1048 of the file wp-content/themes/ActifsDV/functions.php. Please correct it and try to save again.
Cannot redeclare sti() (previously declared in wp-content/themes/ActifsDV/functions.php:1048)It seems kind of weird, because the mistake seems to happen on the same line that the function sti is first declared ?
Thanks for your help !
September 13, 2021 at 9:38 pm #1320807your error message is a classic error message if a function already exists.
So on top the name of the functions are: avia_blog_post_query_mod and bump_sticky_posts_to_topif you use them again ( see line 1048) – it is previously declared !
rename your new entries
f.e.:add_filter('avia_post_slide_query', 'avia_post_slide_query_mod', 10, 2); function avia_post_slide_query_mod($query, $params) { $include = array(); $sticky = get_option( 'sticky_posts' ); $args = array( 'taxonomy' => $params['taxonomy'], 'post__not_in' => $sticky, ); $posts = get_posts( $args ); foreach($posts as $post) { $include[] = $post->ID; } $include = array_merge($sticky, $include); // convert values of the $include from string to int function sti($n) { settype($n, 'int'); return $n ; } $include = array_map("sti", $include); $query['post__in'] = $include; $query['posts_per_page'] = 6; $query['orderby'] = 'post__in'; // sort items based on the post__in value return $query; }
__________________
by the way – there is for masonries a similar solution with filter: avia_masonry_entries_query___________________
and maybe this post is helpful too – https://kriesi.at/support/topic/sticky-posts-in-post-slider/#post-1202275 and the next post is to have on post slides that too.
you can give to single posts that are set as sticky posts a class – to easier style them:add_filter( 'post_class', 'add_sticky_classes', 10, 3 ); function add_sticky_classes( $classes, $class, $post_id ) { if ( ! is_sticky() ) { return $classes; } global $wp_query; $classes[] = 'stickypost-' . $post_id; return $classes; }
September 14, 2021 at 12:35 pm #1320888Hi Guenni,
Thanks for your help, but I’m afraid I wasn’t clear. When I switched the filter, I didn’t paste a new function, but changed the old one – and I do not have the two functions active at the same time (I comment out the bump_sticky_posts_to_top when I test the other one).
So the problem wasn’t with me having two avia_blog_post_query_mod functions.No, the interesting thing is when I paste the code (for the avia_blog_post_query_mod function), it tells me that the sti function (included in the code) causes the problem. When I change the name of the main function (avia_blog_post_query_mod) this doesn’t change. And when , in the code, I change the name of the sti function (to for example stipendous), the error message changes too and tells me stipendous is already declared on line 1048 so I cannot redeclare it on line 1048.
What is annoying is that I tried copying the code on another website, and the error doesn’t happen, which makes sense – but why is it not working on this website ?
Thanks for the help !
September 15, 2021 at 12:52 pm #1321030Hi,
Thank you for the updat.e
We may have to access the site in order to check the issue properly. Please post the login details in the private field, and make sure that the Appearance > Editor panel is accessible.
Best regards,
IsmaelSeptember 17, 2021 at 1:16 pm #1321377Hi,
Thanks for your help again. I was able to get rid of the error message – I just got the sti function “out” (see below) -, but the sticky post are not in first position in the grid (see links and explanations in private), which was the point of it all… Any idea ?
Thanks again !
// sticky posts function stipendus($n) { settype($n, 'int'); return $n ; } add_filter('avia_post_slide_query', 'avia_post_slide_query_mod_jm', 10, 2); function avia_post_slide_query_mod_jm($query, $params) { $include = array(); $sticky = get_option( 'sticky_posts' ); $args = array( 'taxonomy' => $params['taxonomy'], 'post__not_in' => $sticky, ); $posts = get_posts( $args ); foreach($posts as $post) { $include[] = $post->ID; } $include = array_merge($sticky, $include); // convert values of the $include from string to int $include = array_map("stipendus", $include); $query['post__in'] = $include; return $query; }
September 20, 2021 at 7:17 am #1321573Hi,
Thank you for the info.
Is the sticky post positioned at the very last of the array? Please try to use the array_reverse function to adjust the order of the $include array.
// https://www.php.net/manual/en/function.array-reverse.php
Or replace the array_merge with the array-unshift function to move the sticky post at the beginning of the array.
// https://www.php.net/manual/en/function.array-unshift.php
Best regards,
IsmaelSeptember 20, 2021 at 11:11 am #1321596Hi Ismael,
I’ll try. Thanks for your help, and thanks also to Guenni007 (I’ve seen a lot of your posts on these forums, and I appreciate all that you do for us Enfold users !)Cheers !
September 24, 2021 at 5:40 am #1322095 -
AuthorPosts
- You must be logged in to reply to this topic.