-
AuthorPosts
-
October 5, 2021 at 3:22 pm #1323659
Hey,
on our news page, we have a masonry gallery that outputs blog posts with categories.
When I enable the sticky post option for a blog post it doesn’t get shown at the beginning of the grid.
Thanks in advance
Kind regards
October 6, 2021 at 9:55 am #1323723Hey emilconsor,
Thank you for the inquiry.
Sticky posts are not included in the masonry’s default query, so you will have to adjust the query manually using the avia_masonry_entries_query filter.
add_filter("avia_masonry_entries_query", function($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['orderby'] = 'post__in'; // sort items based on the post__in value return $query; }, 10, 2);
Related thread: https://kriesi.at/support/topic/sticky-post-in-blog-grid/#post-1320807
Best regards,
IsmaelOctober 11, 2021 at 2:23 pm #1324425Hi Ismael,
thanks for the snippet, this works – but only kind of.
The sticky post is on top – which is what we wanted. But the post will be displayed twice once you “load more” entries. Is there a way to exclude the sticky post from the query once it’s “on top”?- This reply was modified 3 years, 2 months ago by emilconsor.
October 13, 2021 at 12:04 am #1324646Hi,
Thank you for the update.
We could exclude the sticky posts when loading more posts but we have to modify the config-templatebuilder/avia-shortcodes/av-helper-masonry.php file and replace line 1565..
$query = apply_filters( 'avia_masonry_entries_query', $query, $params );
,.. with:
$query = apply_filters( 'avia_masonry_entries_query', $query, $params, $ajax );
We can then use the post__not_in parameter to exclude the sticky posts when loading more posts.
add_filter("avia_masonry_entries_query", function($query, $params, $ajax) { if($ajax) { $query["post__not_in"] = array(1, 254); } return $query; }, 10, 3);
Make sure to replace the array value with the actual IDs of the sticky posts.
Best regards,
IsmaelOctober 13, 2021 at 3:18 pm #1324764Hi Ismael,
thanks for that. I tried overriding the av-helper-masonry by creating a shortcodes-directory in my child theme and loading them (just as described in your documentation with “avia_load_shortcodes”) but that doesnt override the helper. Is there another way to accomplish that? I would prefer to not touch the parent theme.
Best regards
October 14, 2021 at 7:45 am #1324854Hi,
It is working correctly on our end. Did you add the $ajax parameter in the avia_masonry_entries_query within the query_entries function or method? There are two instances of the avia_masonry_entries_query filter in the av-helper-masonry.php file.
Best regards,
IsmaelOctober 14, 2021 at 10:17 am #1324890Hi Ismael,
strangely it is not on my end.
I’ve changed the filter in the functions.php toadd_filter("avia_masonry_entries_query", function($query, $params, $ajax = false) { if($ajax) { $sticky = get_option( 'sticky_posts' ); $query["post__not_in"] = $sticky; } return $query; }, 10, 3);
otherwise my page was broken.
Yes, I did add that. Do you have an idea why it is not working on my end?
October 14, 2021 at 10:49 am #1324896Sorry, it is working now. My fault!
Thank you Ismael.October 16, 2021 at 4:10 am #1325196 -
AuthorPosts
- The topic ‘Sticky Blogposts when using a masonry gallery’ is closed to new replies.