- All Things Seen and Unseen - http://rmarsh.com -

Similar Posts: WordPress Plugin

Updated

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 [1] where future developments will be documented.

Comments are now closed for this post but can be added to the new page [1].

Description

I’ve been looking [8] for a better alternative to the Related Posts [9] 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).

Instructions

  1. Download the latest version of Similar Posts [10].
  2. 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)
  3. 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.
  4. Put 

    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

  5. and
  6.  

    but that can be changed. Use the Admin|Options|Similar Posts page to set how you want the list of posts displayed.

Acknowledgements

Similar Posts is based on Related Posts [9] 2.02 by Alexander Malov and Mike Lu. I’ve also used some code from Rich Boakes [11] and Ken Cheung [12].

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&#038;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):

limit
maximum number of posts to show (5)
skip
how many posts to skip before listing (0)
show_static
include static pages (false)
show_private
include password-protected posts (false)
excluded_cats
comma separated list of categories to exclude (by ID) (9999, the default means none)
excluded_authors
comma separated list of authors to exclude (by ID) (9999, the default means none)
none_text
what to show if no posts match–can be plain text or a permalink
before_title
what to show before a link ()
after_title
what to show after a link( 

)

trim_before
remove the first instance of ‘before_title’ (false)
show_excerpt
include a snippet of the post after the link (false)
excerpt_length
how long an excerpt should be (50 characters)
excerpt_format
‘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.
ellipsis
add ‘ …’ after the excerpt
before_excerpt
what to show before an excerpt ()
after_excerpt
what to show after an excerpt ( 

)

Feedback

If you try this plugin leave a comment here to let me know how you get on.