Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #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

    #1323723

    Hey 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,
    Ismael

    #1324425

    Hi 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, 1 month ago by emilconsor.
    #1324646

    Hi,

    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,
    Ismael

    #1324764

    Hi 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

    #1324854

    Hi,

    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,
    Ismael

    #1324890

    Hi Ismael,
    strangely it is not on my end.
    I’ve changed the filter in the functions.php to

    add_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?

    #1324896

    Sorry, it is working now. My fault!
    Thank you Ismael.

    #1325196

    Hi,

    Glad to know that it is now working and looks like you have created a better solution. Thanks for sharing! We will close this thread for now.

    Have a nice day.

    Best regards,
    Ismael

Viewing 9 posts - 1 through 9 (of 9 total)
  • The topic ‘Sticky Blogposts when using a masonry gallery’ is closed to new replies.