Last active
October 12, 2017 21:10
-
-
Save mgibbs189/d961021b74b2999122a55ae378cf9f65 to your computer and use it in GitHub Desktop.
Always show featured (_featured) items first
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment