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
  • So, the output you are trying to have is like this - <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 the randomize_order_with_sticky_on_top function? – Sally CJ Commented Feb 6, 2024 at 0:46
  • It would be <sticky posts> <randomized posts with all blue in the top 50%>. I'm using this plugin to call the function nsp-code.com/… – dagnabbit Commented Feb 6, 2024 at 20:38
Add a comment  | 

1 Answer 1

Reset to default 0

Issue 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;
}

本文标签: