WordPress Tips

Remove WordPress REST API links

When using the WordPress REST API, it might be needed to hide some WP REST API links from the page source to the outside world. Here’s how to do that.

Remove WordPress REST API Links

Let’s say you have added your own endpoint to the WP REST API, and do not want to publicly show it in the page source. There are two places the WP REST API link is added to the page source:

To remove these links we simply remove the functions that are hooked to wp_head and template_redirect

// Hide WP REST API links in page headers
remove_action( 'wp_head', 'rest_output_link_wp_head', 10);
remove_action( 'template_redirect', 'rest_output_link_header', 11);

Be aware that if REST API calls are done via AJAX, the browser shows the URL that is called, and it is visible to the public.

Bonus: Clear the WP-JSON index

If you have the WP REST API active, and don’t want people to see what endpoints are available, use below code.

// Hide WP REST API JSON index
add_filter('rest_index', 'nstrm_hide_wp_json_index');
function nstrm_hide_wp_json_index( $response ){
	return array();

If you want to further customize the WP REST API, use the plugin Disable REST API. This plugin disables all endpoint of the WP REST API and allows you to enable individual endpoints.

Screenshot of settings for the Disable REST API plugin
Settings of the Disable REST API plugin.

Breaking through the fog

WordPress Tips

Using FacetWP to show featured posts first

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, becuase ACF stores the value of the uitgelicht (Featured) 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 seting 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. 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.

Here the code on Github, feel free to comment if you want.

Need technical help with WordPress?

Do you need technical help with WordPress? Please let me know.

Photo by Susan Yin on Unsplash

WordPress Tips

Preselecting a facet and hiding it automatically with FacetWP

In this short howto I will show you how you can preselect a facet, and hide it when the page renders.

Preselecting facets

For this case the client has a few facets that are filled by custom fields. The fields are specialism and level. These a are related to a custom post type Mentor.

To preselect a facet, we can use the facetwp_preload_url_vars filter. But, during execution the $post global variable is not available, so we need to first get the post. By using the get_uri() method of the FWP->helper class, we can get the uri. This can be mapped to the page name which a separate function nostromo_get_post_id_by_post_name() uses to get the post id.

When we have the post id, it’s possible to get the custom fields and use them as values for the preselects. We just have to add them to the url_vars[] array with the names of the facets.

At the end of the function we return the url_vars array, so the filter can be applied.

Hiding the preselected facet

To hide the preselected facet(s), all we have to is add a piece of CSS to the page, when the url_vars are set. Here’s when a hook comes in very handy. You can see the code on lines 25 and 36. You might need to tweak the CSS, or add some classes to make the CSS specific enough to work. Or use !important, just don’t tell anyone I gave you that advice.

Need technical help with WordPress?

Do you need technical help with WordPress? Please let me know.




Blue and orange


Good morning


Spring is coming


Foggy countryside


Sunrise and ice