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;
} );
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.