Support

Account

Home Forums Front-end Issues Display Featured Items First Reply To: Display Featured Items First

  • You should use php’s usort. The code below:

    1. creates a function that grabs the featured post’s ID (set in a custom field assigned to the posts page)
    2. checks posts in the query and compares two at a time, moving $post_a to the front if its ID matches the featured post ID.

    usort( $wp_query->posts, function ( $post_a, $post_b ) {
        
        $featured = get_field('featured_post', $page)[0];
    
        $a = $post_a->ID == $featured;
        $b = $post_b->ID == $featured;
    
        if ( !$a && !$b ) { // both false
            return 0;
        }
        return ( $a ) ? - 1 : 1;
    } );