Skip to content

Using FacetWP to show featured posts first

November 3rd, 2019 WordPress Tips Lots of books in a library

Want to show featured posts first in FacetWP results? Here’s how to do it.

The requirement

A client, using FacetWP to show posts filtered by a few Advanced Custom Fields, wanted to introduce featured posts. These posts need to be show first, and the rest of the results should be shown random.

Since FacetWP does an AJAX request every time a page is requested, or a facet is changed, we need to do something to prevent every request to show random posts, since that could show posts on multiple result pages.

Thinking about a solution

When a client drops an idea, actually any idea, my brain starts to think on how to implement this. With my experience I know some filters of my favorite plugins, but not all. So I draft a solution first.

Posts need to have an extra field, where the client can indicate a post is featured. Since the posts already have some ACF fields, I will add the featured field to it.

Next up is filtering the query to feed FacetWP the $post_ids in the right order, with featured posts first, followed by the other posts in a random order. I found out the facetwp_filtered_post_ids filter can be used for that.

Coding the solution

Here’s my favorite part of implementing a clients request. The actual coding. I start with writing the facetwp_filtered_post_ids filter function. This function has to return all post ids, in the right order, with the featured post on the front of the array.

The query arguments are self explanatory, except maybe for the meta_compare argument. LIKE is used here, because ACF stores the value of the uitgelicht (Featured in Dutch) field in a serialized way. Be careful when using this, since this might give you unexpected results. In this case, the uitgelicht field can only have a value of 'ja' (yes), or '' (empty), so we can safely use this.

The we launch the query and get the results on line 16. NOw we need to remove the featured posts from the $posts_ids variable that is passed to this function through the filter. So we walk through the $post_ids and put the featured post id in the $matches array, and remove the post id from the $post_ids array. This gives us two arrays, $matches which holds the featured posts, and $post_ids which holds all other posts, without the featured posts.

Now we need to randomize the posts in $post_ids. As stated before, we don’t want to randomize on every request. With the client I discussed this, and came to a solution where results would be randomized every hour.

First I used the shuffle() function to randomize the $post_ids array. Sadly, this did not give me an option to keep the random order for a certain amount of time. So, here comes array_multisort().

We first set a duration in minutes, get the current time in minutes and subtract the duration from this time, to set the seed. Then we use the mt_srand() function to create seed. Next is setting the order and use that to re-order the $post_ids array with array_multisort().

The final thing we need to do in this filter function is merge the randomized post ids, together with the featured posts and return the array.

🚀 Performance tip

If you only need post ids, add 'fields' => 'ids' to the query $args array. This results in a query that only asks for ids, and not all other fields available in the posts table.

Apply the sorting to the FacetWP template

One final step is needed to get the results right. We need to set the order and orderby parameters on the FacetWP query for the template we want this to be used for. Here’s the facetwp_query_args filter. We add the query parameters to the original query parameters, and return the $query_args variable.

2// put featured posts first in FacetWP results
3add_filter( 'facetwp_filtered_post_ids', function ( $post_ids, $class ) {
5 // Lookup projects with featured meta key set to ja
6 $args = array(
7 'post_type' => 'post',
8 'post_status' => 'publish',
9 'meta_key' => 'uitgelicht',
10 'meta_value' => 'ja',
11 'meta_compare' => 'LIKE',
12 'fields' => 'ids',
13 );
15 $featured_posts_query = new WP_Query( $args );
16 $featured_posts_array = $featured_posts_query->posts;
18 // If a featured post, remove it from $post_ids and add it to $matches
19 // We loop through $post_ids to preserve featured order
20 $matches = array();
21 foreach ( $post_ids as $key => $post_id ) {
22 if ( in_array( $post_id, $featured_posts_array ) ) {
23 $matches[] = $post_id;
24 unset( $post_ids[ $key ] );
25 }
26 }
28 // randomize post_ids
30 $duration = 60; // in minutes, so 60 is one hour.
31 $mins = date('i', strtotime('now'));
32 $seed = $mins - ($mins % $duration);
34 mt_srand($seed);
35 $order = array_map(function() {return mt_rand();}, range(1, count($post_ids)));
36 array_multisort($order, $post_ids);
38 // Featured first, then default sort
39 $post_ids = array_merge( $matches, $post_ids );
41 return $post_ids;
42}, 15, 2 );
44add_filter( 'facetwp_query_args', 'nostromo_filter_posts', 10, 2 );
45function nostromo_filter_posts( $query_args, $class ) {
46 // only use these extra query args if we are using this FacetWP template
47 if ( 'posts_overview' == $class->template[ 'name' ] ) {
49 // add the sorting
50 $query_args[ 'order' ] = 'ASC';
51 $query_args[ 'orderby' ] = 'post__in meta_value';
52 }
54 return $query_args;

Featured image by Susan Yin on Unsplash

About Marcel Bootsman

Marcel discovered the web in 1995. Since then he has paid attention to and worked with lots of technologies and founded his own WordPress oriented business in 2009.

Currently Marcel is Marketing Manager for the Dutch Market at Kinsta where he helps Kinsta's client base grow in the Managed WordPress and Application & Database hosting arena.

You can contact Marcel on a diverse range of online platforms. Please see the Connect section on the homepage for the details.