Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1138279

    i want to have one sticky post always shown. ( or include alway a specific post – and show the rest randomized )
    best would be to exclude this post from randomized array then .

    i use for randomization this snippet:

    add_filter('avia_post_slide_query','avia_order_by_random', 10, 2);
    function avia_order_by_random($query, $params) {
      $query['orderby'] = 'title';
      $query['orderby'] = 'rand';
    return $query;
    }

    when i have this:

    add_filter('avia_post_slide_query','avia_order_by_random', 10, 2);
    function avia_order_by_random($query, $params) {
      $query['orderby'] = 'title';
      $query['orderby'] = 'rand';
      $query['post__in'] = get_option( 'sticky_posts' );
    return $query;
    }

    only the sticky posts are shown – thats clear – is there a way to show first sticky post and then randomize the Rest?

    • This topic was modified 5 years, 2 months ago by Guenni007.
    #1139293

    Hey Guenter,

    Thank you for the inquiry.

    You may need to add more than one blogs posts element in order to do that. Adjust the query of the first element using the designated filter. Usage example can be found in the following thread.

    // https://kriesi.at/support/topic/sticky-posts-cant-make-it-work-on-homepage/#post-1138963

    Best regards,
    Ismael

    #1139557

    thanks Ismael
    and sorry for late reply.

    • This reply was modified 5 years, 2 months ago by Guenni007.
    #1139630

    so i managed it an analog way on that link with:

    add_filter('avia_post_slide_query','avia_order_by_random', 10, 2);
    function avia_order_by_random($query, $params) {
    	
    	$include = array();
    	$sticky = get_option( 'sticky_posts' );
    
    	$args = array(
    	'taxonomy' => $params['taxonomy'],
    	'post__not_in' => $sticky,
    	'orderby' => 'rand',
    	);
    	$posts = get_posts( $args );
    
    	foreach($posts as $post) {
    	$include[] = $post->ID;
    	}
    
    	$include = array_merge($sticky, $include);
    
    	$query['post__in'] = $include;
    	$query['orderby'] = 'post__in';
    
    return $query;
    }

    i had that extra code

    function ava_exclude_current_post($query) {
      if (is_singular('post')) {
        $exclude = avia_get_the_ID();
        $query->set( 'post__not_in', array($exclude) );
      }
    }
    add_action('pre_get_posts', 'ava_exclude_current_post');

    but now my exclusion of the current single post id is ignored by that above. How to include that in the code?

    #1139638

    it works fine, unless I call the sticky post, it will be listed in the slider.
    Can I still exclude it if that single post is displayed?

    add_filter('avia_post_slide_query','avia_order_by_random', 10, 2);
    function avia_order_by_random($query, $params) {
    	
    	$include = array();
    	$exclude = avia_get_the_ID();
    	$sticky = get_option( 'sticky_posts' );
    
    	$args = array(
    	'taxonomy' => $params['taxonomy'],
    	'post__not_in' => $sticky,
    	//  'post__not_in' => $exclude,
    	'orderby' => 'rand',
    	);
    	$posts = get_posts( $args );
    
    	foreach($posts as $post) {
    	$include[] = $post->ID;
    	}
    
    	$include = array_merge($sticky, $include);
    
    	$query['post__in'] = $include;
    	$query['orderby'] = 'post__in';
    	
    return $query;
    }
    
    #1139864

    Hi,

    We should be the one apologizing. We replied 4 days after your post.

    Anyway, you may need to wrap this code inside an !is_single conditional function.

    $include = array_merge($sticky, $include);
    

    You’re adding the sticky post in the post__not_in parameter, but then the line above still merges the sticky post in the array.

    Thank you for the update.

    Best regards,
    Ismael

    #1139875

    now to the side issue.
    Here it’s actually ok everywhere, except for the Sticky Post.
    Homepage, All “non-sticky” Posts and the “Blog” Page are ok. But on that Sticky-Post you can see this Sticky Post in the Postslider too. That would be perfect if it didn’t show up down there.

    Edit : i did it this way:

    	if ( !is_sticky() ) {
    		$include = array_merge($sticky, $include);
    	};

    can be closed now – thanks for your help

    #1140089

    Hey!

    But on that Sticky-Post you can see this Sticky Post in the Postslider too.

    Have you tried using the !is_single function to wrap the array_merge?

    Cheers!
    Ismael

    #1140145

    Sorry Ismael it is solved with the is not sticky conditional.
    There is only one sticky post – and if i have this sticky post open – i do not want to see it in the post slider.
    Can be closed

    #1140620

    Hi,

    Thanks for the update @guenni007, I’ll go ahead and close this thread for now then.

    Best regards,
    Rikard

Viewing 10 posts - 1 through 10 (of 10 total)
  • The topic ‘Randomize post slider but…’ is closed to new replies.