Print Version October 4th, 2006
13th January, 2007
Similar Posts Version 2.0.0 beta is now available. Largely rewritten, it includes better handling of extended character sets, some new options, and many more possible styles of display. Version 2 now has its own page where future developments will be documented.
Comments are now closed for this post but can be added to the new page.
- Version 1.14 just fixes two bugs. The Posts plugin were not working if more than one was installed! Also some users were getting odd characters appearing where they shouldn’t.
- Version 1.13 allows some parameters (like ‘before_title’) to be blank, or to be more complex, e.g.,
. Also allows trimming an excerpt so it ends with a word or a sentence and not in mid-word. NB excerpt_length is now counted in characters and not words as previously.
- Version 1.12 fixes a bug with the option to show static pages.
- Version 1.11 improves the sanity checking of parameter values to avoid database errors. Also lets you skip over a number of posts if you so wish (though this makes more sense for the related Recent Posts plugin).
- Version 1.10 adds the ability to exclude certain authors on a multi-author blog. The text to show if no matches are found is now customisable and there is a new option for displaying links. All the options can be set via the options page but new in this version they can also be specified via a query-style parameter. This gives the flexibility to use Similar Posts in several places with different behaviour. I have also built three new plugins which use the same infrastructure: Random Posts, Recent Posts, and Recent Comments.
- Version 1.03 adds the ability to exclude static pages or certain categories of post. It also has an improved stopword list based on the one used by MySQL.
- Version 1.02 is a bug-fix and security release. It protects against a vulnerability where stray characters in the matching terms could cause database errors. This update also fixes a potential naming conflict between the internal names of its options and those of other plugins and establishes sensible default values for these options. When you update the installation you should visit the options page and check the settings are to your liking.
- Version 1.01 restores the ability to use keywords or otherwise tweak the terms used to find similar posts. It also allows you to import keywords previously assigned using the Related Posts plugin.
- Version 1.00
I’ve been looking for a better alternative to the Related Posts plugin for WordPress. At least better on a blog like mine … and maybe yours too. Related Posts finds matches to other posts using the post’s title or any keywords that you care to define. The titles of my posts unfortunately reflect their context rather than their content and I have too many of them to go back and decide on keywords for them all. So I wrote a new plugin, Similar Posts,using a different algorithm which finds related posts based on the contents of a post and the pattern of word-usage rather than just its title.
You can see it in operation in the ‘Related Reading’ section of my sidebar (on single-post pages).
- Download the latest version of Similar Posts.
- Upload the whole plugin folder (Similar_Posts) to your /wp-content/plugins/ directory. (Similar Posts can be installed without uninstalling Related Posts if you want to try out the difference)
- Go to your Admin|Plugins page and activate Similar Posts. This will automatically add an index to your posts table to enable fast matching. Don’t be alarmed if this takes a few moments.
at the place in your WP loop where you want the list of similar posts to appear. By default the plugin wraps each post with
but that can be changed. Use the Admin|Options|Similar Posts page to set how you want the list of posts displayed.
Under the Hood
By default Similar Posts scans a post each time it needs to find words to match. For long posts this can add an unwanted overhead. The plugin can speed up the process by caching the search terms as a custom field (named ‘similarterms’). When a post is published for the first time or subsequently edited the custom field gets updated. Since you probably have a lot of posts you won’t want to edit each one manually to cache the terms. Instead, the Admin|Options|Similar Posts page lets you process all your posts in one go. It won’t overwrite any terms that are already cached so there is also a button to clear all terms.
While you are editing a post you can modify the ‘similarterms’ custom field to add keywords or replace the automatically generated terms altogether. Note that the field will not be visible until you have saved the post at least once. If you ever want to regenerate the default terms just delete the current set.
If you have previously used the Related Posts plugin to assign keywords to posts you can now import them all from the Options page.
The Similar_Posts folder contains a file, ‘en.words.php’, which is used to supply the ‘stop list’ of common words that you want to exclude as search terms. It is supplied this way so that if your blog is in a different language you can use an alternative stop list. Similar Posts checks the WPLANG constant to see which language WordPress is using and looks for a file on that basis. For example, if the the language code is ‘fr’ for French, Similar Posts will look for a file ‘fr.words.php’. The language files must be in the same directory as similar-posts.php.
The way the list is displayed can be set from the Options|Similar Posts
page. You can exclude certain categories of post, for example, or
change the code that comes before and after the link.
These general options can be overridden in specific cases by passing a query-style parameter, e.g.:
<!--?php random_posts('limit=10'); ?-->
- lists 10 random posts
<!--?php random_posts('none_text=sorry&show_static=false'); ?-->
- lists the default number of posts, excluding static pages, and specifies what to display if there are none
If you do not specify an option its value is taken from the options page.
This means you can use the template tag in different ways in different places.
The full list of parameters is as follows (with the default value in parentheses):
- maximum number of posts to show (5)
- how many posts to skip before listing (0)
- include static pages (false)
- include password-protected posts (false)
- comma separated list of categories to exclude (by ID) (9999, the default means none)
- comma separated list of authors to exclude (by ID) (9999, the default means none)
- what to show if no posts match–can be plain text or a permalink
- what to show before a link (
- what to show after a link(
- remove the first instance of ‘before_title’ (false)
- include a snippet of the post after the link (false)
- how long an excerpt should be (50 characters)
- ‘char’, the default, does nothing, ‘word’ trims the excerpt to the last full word, and ‘sent’ to the full sentence. If the excerpt would be trimmed to nothing no trimming is applied.
- add ‘ …’ after the excerpt
- what to show before an excerpt (
- what to show after an excerpt (
If you try this plugin leave a comment here to let me know how you get on.