admin管理员组文章数量:1126077
I'm using a plugin that lets me order posts and set some as "sticky" and always on top.
I have the example code below that will randomize all posts except for the sticky posts, they will remain on top.
function randomize_order_with_sticky_on_top ( $post_list, $sort_view_id, $orderBy, $query )
{
shuffle($post_list);
//retrieve the sticky list for current sort ID
$sticky_list = get_post_meta( $sort_view_id , '_sticky_data', TRUE);
if ( ! is_array ( $sticky_list ) || count ( $sticky_list ) < 1 )
return $post_list;
//remove the sticky items in the $post_list
foreach ( $sticky_list as $position => $object_id )
{
if ( isset ( $post_list [ $object_id ] ) )
unset ( $post_list [ array_search( $object_id, $post_list ) ] );
}
$post_list = array_values ( $post_list );
foreach ( $sticky_list as $position => $object_id )
{
// Insert the ID at the specified position
array_splice( $post_list, $position - 1, 0, $object_id );
}
return $post_list;
}
However I have an Advanced Custom field post type that can have multiple values (it's a checkbox) lets say the acf field is "color" and the possible values are red, green, blue. Since it's a checkbox it can be any one of these ore multiple and return an array of colors. I'm trying to make it so it will guarantee all of those that include blue (so not just blue but if they're also red and blue) will all be among the top 50% of the randomized order. I've tried multiple variations of this but I either break the sticky or the blue items are randomized throughout.
I'm using a plugin that lets me order posts and set some as "sticky" and always on top.
I have the example code below that will randomize all posts except for the sticky posts, they will remain on top.
function randomize_order_with_sticky_on_top ( $post_list, $sort_view_id, $orderBy, $query )
{
shuffle($post_list);
//retrieve the sticky list for current sort ID
$sticky_list = get_post_meta( $sort_view_id , '_sticky_data', TRUE);
if ( ! is_array ( $sticky_list ) || count ( $sticky_list ) < 1 )
return $post_list;
//remove the sticky items in the $post_list
foreach ( $sticky_list as $position => $object_id )
{
if ( isset ( $post_list [ $object_id ] ) )
unset ( $post_list [ array_search( $object_id, $post_list ) ] );
}
$post_list = array_values ( $post_list );
foreach ( $sticky_list as $position => $object_id )
{
// Insert the ID at the specified position
array_splice( $post_list, $position - 1, 0, $object_id );
}
return $post_list;
}
However I have an Advanced Custom field post type that can have multiple values (it's a checkbox) lets say the acf field is "color" and the possible values are red, green, blue. Since it's a checkbox it can be any one of these ore multiple and return an array of colors. I'm trying to make it so it will guarantee all of those that include blue (so not just blue but if they're also red and blue) will all be among the top 50% of the randomized order. I've tried multiple variations of this but I either break the sticky or the blue items are randomized throughout.
Share Improve this question asked Feb 5, 2024 at 21:53 dagnabbitdagnabbit 111 bronze badge 2 |1 Answer
Reset to default 0Issue with the way you are unsetting sticky posts from the $post_list
array and then reinserting them at the specified positions and should work correctly like below code structure.
Removed the unnecessary $position
variable in the foreach
loop.
Changed the way sticky items are inserted back into the $post_list array using array_unshift
to add them at the beginning of the array
function randomize_order_with_sticky_on_top( $post_list, $sort_view_id, $orderBy, $query ) {
shuffle( $post_list );
// Retrieve the sticky list for the current sort ID
$sticky_list = get_post_meta( $sort_view_id, '_sticky_data', TRUE );
if ( ! is_array( $sticky_list ) || count( $sticky_list ) < 1 ) {
return $post_list;
}
// Remove the sticky items from the $post_list
foreach ( $sticky_list as $object_id ) {
$index = array_search( $object_id, $post_list );
if ( $index !== false ) {
unset( $post_list[ $index ] );
}
}
// Reset array keys
$post_list = array_values( $post_list );
// Reinsert sticky items at the beginning of the $post_list
foreach ( $sticky_list as $object_id ) {
array_unshift( $post_list, $object_id );
}
return $post_list;
}
本文标签:
版权声明:本文标题:advanced custom fields - Randomize post list with some posts with specific ACF value in the top 50% 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736662169a1946467.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<Sticky posts> <"Blue" posts> <All other posts>
- right? What is$post_list
? I mean, can you show an example of how are you defining that variable and then calling therandomize_order_with_sticky_on_top
function? – Sally CJ Commented Feb 6, 2024 at 0:46