Always show featured (_featured) items first

mgibbs189

{{gist file=”test.php” lang=”php”}}
<?php

// Add the following to your (child) theme’s functions.php
add_filter( ‘facetwp_filtered_post_ids’, function( $post_ids, $class ) {
global $wpdb;

// Lookup featured post IDs
$sql = ”
SELECT post_id
FROM {$wpdb->prefix}postmeta
WHERE meta_key = ‘_featured’ AND meta_value = ‘1’ AND post_id IN (” . implode( ‘,’, $post_ids ) . “)”;
$results = $wpdb->get_col( $sql );

// If a featured post, remove it from $post_ids and add it to $matches
// We loop through $post_ids to preserve featured order
$matches = array();
foreach ( $post_ids as $key => $post_id ) {
if ( in_array( $post_id, $results ) ) {
$matches[] = $post_id;
unset( $post_ids[ $key ] );
}
}

// Featured first, then default sort
$post_ids = array_merge( $matches, $post_ids );

return $post_ids;
}, 15, 2 );

{{/gist}}