How to filter a Facebook feed by a word, string or hashtag, so only the posts you want appear on your site.
Would you like to show only some of the posts from your Facebook Page?
In this guide, we’ll show you how to filter your Facebook feed by a word or hashtag. We’ll also cover hiding a hashtag from your posts and showing only posts with a minimum number of likes or shares. All you need is Custom Facebook Feed Pro and a feed you’ve already created.
Requirements: Post filters require Facebook Feed Pro with the Plus license level or higher. In the free version, the filter fields appear dimmed and can’t be edited.
Before you get started, make sure Custom Facebook Feed Pro is installed and activated on your WordPress site and that your feed is set up.
Here is what we will cover.
- Filtering Posts by Words
- If You Do Not See the Posts You Expect
- Filtering by Multiple Words
- Hiding the Hashtag
- Filtering by Likes or Shares
- Frequently Asked Questions
Filtering Posts by Words
To get started, we’ll set a filter in your feed’s settings. A filter checks the text of each post and keeps or hides it based on the words you enter.
- In your WordPress dashboard, go to Facebook Feed » All Feeds.
- Next, click the name of your feed, or click the Edit icon beside it.
- Then click the Settings tab.
- Finally, open the Filters section.
The Filters section has two boxes for words.
- Only show posts containing shows only the posts that contain your words. Use it when you want a feed about one topic.
- Do not show posts containing hides every post that contains your words. Use it when you want to keep a topic out of your feed.
Next, type your words into a box. To use more than one word or hashtag, separate them with commas. The feed preview updates when you click outside the box.
Here is how the filter reads your words.
- It does not care about capital letters, so
salealso finds Sale. - It matches whole words. The word
saledoes not match a post that only says sales. - It checks the text of each post. For a post without text, it checks the post’s link name and description.
- With several words in the Only show posts containing box, a post stays if it contains any one of them.
- With several words in the Do not show posts containing box, a post is hidden if it contains any one of them.
- When you fill in both boxes, a post must contain one of the first words and none of the second.
To filter by a hashtag, include the # sign, such as #sale. The hashtag then matches only a hashtag, not the plain word. In the same way, @ matches only a mention.
Note: With a feed made in the builder, set filters here and not in the shortcode. A shortcode such as [custom-facebook-feed feed=1] uses the settings you save in the builder.
Once you’re happy with the preview, save your changes. If your feed shows fewer posts than you expected, we’ll look at why next.
If You Do Not See the Posts You Expect
The plugin filters your posts after Facebook sends them. Facebook can’t send only the posts that contain a certain word. The plugin first asks for a batch of posts and then removes those that don’t match.
If your feed shows few posts or none, the batch may be too small to contain enough matches. To ask Facebook for more posts, raise the API Post Limit. It’s set to Automatic by default, so the plugin picks the number of posts for you. Switching it to Manual lets you choose a higher number yourself.
- In the Settings tab of your feed, open the Advanced section.
- Next, find API Post Limit and choose Manual.
- Then enter a higher number. The most you can ask for is 100.
A higher number works best when kept only as high as you need. A bigger batch means more posts to load, which can make your page appear more slowly.
Words separated by commas are only one way to filter. Next, we’ll look at showing posts that match more than one word.
Filtering by Multiple Words
Words separated by commas work as “or”. A post that contains any one of them is shown. To show only posts that contain two words or hashtags at the same time, you can add a small piece of JavaScript.
First, set the first word in Only show posts containing. Then save your changes. After that, give your feed a class so the code can find it. To do this, add the class to your shortcode, like this.
[custom-facebook-feed feed=1 class="feed1"]
If you add the feed with the Facebook Feed block, enter feed1 in the block’s Additional CSS class(es) box instead.
Next, add the following code with WPCode, as shown in our guide on moving custom JavaScript code out of our plugins. In WPCode, add the code to the Footer box in the Header and Footer area. Replace word2 with your second word, in lowercase. The code turns the post text into lowercase before it looks for your word, so a lowercase word matches whatever capital letters the post uses.
<!-- Custom Facebook Feed JS -->
<script type="text/javascript">
function cff_custom_js($){
var $ = jQuery;
$('.feed1 .cff-item').each(function(){
if( $(this).find('.cff-text').text().toLowerCase().indexOf("word2") < 0 ) {
$(this).remove();
}
});
}
cff_custom_js(jQuery);
</script>
The code goes through each post in the feed with the class feed1. It removes every post whose text does not contain your second word.
To do the same for another feed, copy the five lines that start with $('.feed1 .cff-item') and paste them right below. Then change the class and the word in the copy.
Every snippet in this guide uses the same function, cff_custom_js. When the Load More button adds posts, the plugin runs only the last copy of that function. To use more than one snippet, put all of their lines inside one function in the same way.
JavaScript can also tidy the text of your posts. Next, we’ll hide the hashtag you filtered by.
Hiding the Hashtag
When you filter by a hashtag, every post shows that hashtag. To hide it from the post text, add this code with WPCode in the same way. Replace #put_hashtag_here with your own hashtag.
<!-- Custom Facebook Feed JS -->
<script type="text/javascript">
function cff_custom_js($){
var $ = jQuery;
$('.cff .cff-text').each(function(){
$(this).html( $(this).html().split("#put_hashtag_here").join("") );
});
}
cff_custom_js(jQuery);
</script>
The hashtag in this code is case sensitive, so it must match the capital letters in your posts exactly.
You can use the same approach to filter by popularity. Next, we’ll show only posts with enough likes or shares.
Filtering by Likes or Shares
The filters above work on words. To show only posts with a minimum number of likes or shares, you can use JavaScript again. Add this code with WPCode.
<!-- Custom Facebook Feed JS -->
<script type="text/javascript">
function cff_custom_js($){
var $ = jQuery;
$('.cff .cff-item').each(function(){
var $self = $(this),
likes = parseInt($self.find('.cff-likes .cff-count').text().replace(/\D/g, ''), 10) || 0,
shares = parseInt($self.find('.cff-shares .cff-count').text().replace(/\D/g, ''), 10) || 0;
if( likes < 100 || shares < 30 ) {
$self.hide();
}
});
}
cff_custom_js(jQuery);
</script>
Next, change the numbers to suit your feed.
- The number after
likes <is the fewest likes a post needs. In the example, it is 100. This count is the total of all reactions on the post, not only likes. - The number after
shares <is the fewest shares a post needs. In the example, it is 30. - The symbol between them is either
||or&&. Use||to hide a post that is below the minimum for likes or below the minimum for shares. Use&&to hide a post only when it is below both minimums.
The code reads the counts from the likes and shares shown under each post. For that reason, those counts must stay in your feed. They show by default. To check, go to Customize » Posts » Edit Individual Elements. Here, make sure Likes, Shares and Comments is turned on. If you’d rather not show the counts, you can hide them with CSS. The code still reads them while they stay on the page.
That’s it! You now know how to filter your Facebook feed by words, hashtags, likes and shares. Next, you can style your feed with our custom snippets for Facebook Feed.
Frequently Asked Questions
Below, we’ve answered some of the most common questions about filtering your Facebook posts.
Why is my feed empty after I added a filter?
The plugin filters the posts after Facebook sends them, so it can only filter the batch it received. If none of those posts contain your word, the feed is empty. To fix this, raise the API Post Limit in Settings » Advanced, or check that your word is spelled the way it appears in your posts.
Does the filter find part of a word?
No. The filter matches whole words only. A filter for the word sale does not find a post that only says sales. To find both, add both words to the box, separated by a comma.
Do capital letters matter in a filter?
No. The filter finds the word in any mix of capital and lowercase letters. The code that hides a hashtag is different, because it does care about capital letters.
Why can’t I type in the filter boxes?
The boxes are dimmed in the free version. Post filters are part of Facebook Feed Pro with the Plus license level or higher.
Can I use a filter in the shortcode?
Not for a feed you made in the builder. A shortcode that includes a feed number uses the filters you saved in the feed’s settings.