Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Last active October 12, 2017 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgibbs189/d961021b74b2999122a55ae378cf9f65 to your computer and use it in GitHub Desktop.
Save mgibbs189/d961021b74b2999122a55ae378cf9f65 to your computer and use it in GitHub Desktop.
Always show featured (_featured) items first
<?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