Tagged: , ,

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

    #1320637

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

    #1320731

    Hey 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 !

    #1320807

    your 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_top

    if 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;
    }
    #1320888

    Hi 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 !

    #1321030

    Hi,

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

    #1321377

    Hi,

    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;
    }
    #1321573

    Hi,

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

    #1321596

    Hi 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 !

    #1322095

    Hi,

    No problem. Please let us know if the changes above worked or if you need any help. We will keep the thread open.

    Thank you for your patience.

    Best regards,
    Ismael

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.