-
AuthorPosts
-
September 14, 2019 at 10:34 am #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.
September 18, 2019 at 2:00 am #1139293Hey 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,
IsmaelSeptember 18, 2019 at 4:22 pm #1139557thanks Ismael
and sorry for late reply.- This reply was modified 5 years, 2 months ago by Guenni007.
September 18, 2019 at 6:56 pm #1139630so 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?
September 18, 2019 at 7:23 pm #1139638it 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; }
September 19, 2019 at 11:35 am #1139864Hi,
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,
IsmaelSeptember 19, 2019 at 12:23 pm #1139875now 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
September 20, 2019 at 4:22 am #1140089Hey!
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!
IsmaelSeptember 20, 2019 at 9:09 am #1140145Sorry 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 closedSeptember 22, 2019 at 11:48 am #1140620Hi,
Thanks for the update @guenni007, I’ll go ahead and close this thread for now then.
Best regards,
Rikard -
AuthorPosts
- The topic ‘Randomize post slider but…’ is closed to new replies.