Similarity

I have been using Darren’s Related Posts plugin (DRPP) for a long time. For my purposes it was the best plugin for showing related posts that I had encountered. After a while I decided that I was not completely satisfied with the way that it seemed to favor more recent posts. I developed a new algorithm that I felt would not give preference to newer posts unless they were actually more related.

When I learned that WordPress would be adding native tag support in version 2.3 I decided that I would create my plugin to work with tags after they arrived. Life got busy so I never worked on the plugin until version 2.5 came out (partly because I wanted to convert the post_meta “keywords” from DRPP to tags). I finally buckled down and created my Similarity plugin this week. I have tested it on version 2.3.3 and version 2.5 (I assume it will work with any version of 2.3). I will continue to test it on new versions of WordPress as they are made public. (Przeczytaj opis wtyczki po polsku)

Latest Version 3.0 (April 9, 2012)

Formatting the strength for the auto-generated lists was always listed the strength value regardless of the setting. Thanks to Gerhard for alerting me to the problem.

For those who are curious about why I went to a new major version number for such a minor fix, the reason is that I really should have moved to a new major version number with the functionality added a few releases ago – so better late than never.

To install it simply unzip the file linked above and save it in your plugins directory under wp-content. In the plugin manager activate the plugin. Settings for the plugin may be altered under the Similarity page of the Options menu (version 2.3) or Settings menu (version 2.5 or later).

Options include:

  • Number of posts to show – this is a maximum, it won’t invent connections that don’t exist, set it to 0 (or less) and it will display all matches.
  • Minimum match strength – enter a number between zero and one (one being a perfect match) and items related weaker than the number indicated will not be included – this limit is ignored for the one extra if that option is used.
  • Default Display if no matches – if there are no matches this is what will be displayed, this is not displayed if there are matches, but fewer than the set maximum.
  • Text and Codes before the list – assuming you want to do a list this is where you would place the <ul> or <ol> You may also place any other code you would like to have preceeding the list. (There are two boxes here – one for function calls in your template and one for auto-generated lists.)
  • Text and Codes after the list – this would be the place for </ul> or </ol> You may also place any other code you would like to have following the list.(There are two boxes here – one for function calls in your template and one for auto-generated lists.)
  • Output template – this would be where you place the <li> tags. (There are two boxes here – one for function calls in your template and one for auto-generated lists.) There are also 4 template tags you may use (in any configuration you can imagine) to define how the results are displayed
    • {link} – provides a link – equivelent to <a href=”{url}”>{title}</a>
    • {strength} – outputs the calculated degree of relatedness
    • {url} is the permalink for the related post
    • {title} is the title for the related post
  • Display format for similarity strength – Value displays the {strength} in a decimal format (0.873), Percent displays the {strength} in a percentage format (87.3%), Text displays {strength} as a word (Strong, Mild, Weak, and Tenuous are the defaults), and Visual displays a color block (Green for 100% fading to Yellow and then to Red for weak connections)
  • Relative mixing weights – these values determine the ratio given to the weight of tags vs categories when using the sim_by_mix function. Invalid ratios are treated as a 1 to 1 balance.
  • Custom text for strength – allows you to insert custom text (including markup) for the strength indicator when using the text display format. (Hint: using markup allows for the possibility of showing custom images.)
  • Show one more random related post – dig deeper into the related post list with one random selection (you can set a minimum strength for these random items).
  • Auto-display a Similarity list at the end of a single post – without modifying any templates.

The plugin allows for three function calls anywhere in your page templates or their shortcodes in text widgets (all use the same options):

  • <?php sim_by_tag(); ?> – determines similarity based on the tags applied to the posts (shortcode: SIM-BY-TAG)
  • <?php sim_by_cat(); ?> – determines similarity based on the categories assigned to the posts (shortcode: SIM-BY-CAT)
  • <?php sim_by_mix(); ?> – determines similarity based on the tags and the categories with the relative weights determined by the relative mixing weights setting (shortcode: SIM-BY-MIX)

Alternately you may choose to auto-display any of the similarity lists at the end of posts on single-post pages without getting into the php by selecting the function you wish to auto-display on the Similarity options page.

28 Responses to Similarity

  1. Sven says:

    Hi David,

    Is it possible to exclude certain categories?

    Best,

    Sven

  2. Sylvain says:

    How can I exclude pages from selection??

    • David says:

      I had thought I had provided an option to exclude pages but upon closer inspection I see that the only option I provided was whether to display them on pages or not. I’ll have to add that option.

  3. Anthony says:

    Hi,
    with the automatic method, the format does’nt work.

    In similarity.php line 118, I changed
    $format = stripslashes($options[‘auto_format’]);
    by
    $format = stripslashes($options[‘format’]);

  4. wKad says:

    Hi =) I’m using Similarity 2.13.1 and WordPress 3.0.3. I installed the plugin through the WordPress Admin Plugin menu (not downloading the .zip and uploading it).

    There are no Options nor Settings page on the Plugin, only “Deactivate | Edit”.

    I really want to change some options, but changing on the database don’t work either.

    Am I doing something wrong?

    Thank you~

  5. Oksana says:

    A really great plugin. However, I discovered some drawbacks:
    1) the plugin displays posts which have status ‘Draft’. Is there an option to exclude them from being shown in the similar posts output?
    2) I did not find the option to show post thumbnails. I use custom fields for that, is it possible to fetch similar posts together with cutom field values (for certain custom field keys)?
    3) I would also like to display several words from the post excerpt, not only the post title. Is this possible?

    • David says:

      Some early versions of the post displayed posts that were not yet published but assuming you are using a current version you might want to make sure that it is actually showing those drafts when you are logged out. I haven’t looked at that part of the code recently but I believe I allowed drafts to show if you were logged in as the author of the draft (assuming it would be in the list of similar posts).

      As for your other requests, I have done nothing to handle post thumbnails. I imagine that posts could be compared based on a custom field value but I don’t have the time to try implementing such a thing right now. I know that it would be possible to display an excerpt from the post (I’ve done similar things with other plugins before) but that again is not something I have time for currently. If you or someone you know has PHP & SQL expertise you could certainly try to add those features – I think the code is clearly documented.

  6. Oksana says:

    Indeed, log out helps remove the drafts links. Thanks!

  7. Support Team says:

    Hello,
    We are using the plugin Similarity at our blog http://blog.funmx.com but there is some problem. Would you please like to tell that how to display the related posts in the side bar? As there is no any option showing in the Apperance (Widgets).
    And how will the similar posts display below the posts contents?
    Your swift response required.
    Regards!

    • David says:

      I have not created a widget for similarity. If you wish to use it in the sidebar without editing your sidebar.php you should use a shortcode by typing one of the following between square brackets (eg. [ ]):
      SIM-BY-TAG
      SIM-BY-CAT
      SIM-BY-MIX

      Of course that will only work if shortcodes are enabled in your functions.php file. Let me know if you need help with that.

  8. Olive White says:

    Hi!
    really enjoy this plugin, i would like to add a function post_thumbnail, is it possible? Congrats!

    • David says:

      There is nothing currently to do post thumbnails although I’m sure it could be done. Unfortunately I don’t expect to have time to work on that myself in the near future.

      If anyone else were to get that working before I do I would be happy to include it in a future release.

  9. alqh says:

    Hi,

    I want to display a title, “similar reads” before the list and assign other ul css class to it. Where do I put the codes?

    Thanks

    • David says:

      I would do that kind of thing by adding “Similar Reads {opening list tag with desired class designation}” to the “Text and codes before the list” field and “{closing list tag}” to the “Text and codes after the list” field. Then make sure that you include the desired list element tags in your output template.

  10. Paul Slager says:

    Hi,

    I am not really a programmer at all. I am assuming I have to add the code {link} to the Single_post.php file that my theme uses? Its hard for me to figure out where to add this code at perhaps you can be a bit more descriptive.

    The Auto code works but I dont really like how it looks its to close to the rest of my stuff and I want to put a header on it.

    • David says:

      If you want to edit the Single Post file you would need to put one of the following somewhere below the “while (have_posts())” and above the “endwhile;” on that page:

      OR

      OR

      Regardless of which way you choose to do it the look and format of the similar posts is controlled using the “Text and codes before the list” (which is where you could include a header and BR tags), “Text and codes after the list”, and “Output template” fields on the Similarity Options page. That is where the various template tags such as {link} have any meaning.

  11. novini says:

    Hello i just installed this plugin it working perfect but i want to display thumbs too. How i can do that ? Have you an easy way ?

    • David says:

      I am assuming that you mean to ask if there is an easy way to display post thumbnails. The answer I’d that I have not done anything to facilitate that.

      If you were asking something else please let me know so that I can try to answer the right question.

  12. Rafal says:

    This plugin not work properly :(. She display all posts .. elso not published (sorry my english ;))

  13. kg says:

    Hi David,

    great plugin! Is it possible to exclude certain categories?
    I have 2 categories: Clients and Services. Posts within these two cats share the same tags. Under post from cat Clients I want to show only similar posts from cat Services, and vice versa.

    Many thanks,
    kg

    • David says:

      Could it be done? Probably.

      Is that built in now? No.

      Unfortunately I don’t know when I will have time to add something like that. I will look at adding that when I next update.

  14. opc says:

    Hey David,

    i appreciate your work & plugin every day.

    Is there a simple way to display the publish date beside the post title?

    Thanks for your reply.

  15. pedro says:

    Great plugin, when I add the shortcode to the text widget the ist of posts are displayed outside (and above) the widget. Do you know what may be causing this? I’ve just installed the plugin, I haven’t modified any files.

    Thanks in advance!

    Pedro

    • David says:

      That’s tough to diagnose – it might have something to do with CSS since it sounds like the information is correct even with the gimpy placement. I don’t really have enough information to guess further than that.

Leave a Reply

Your email address will not be published. Required fields are marked *