WordPress.org

Ready to get started?Download WordPress

Plugin Directory

Better Related Posts

Do you use custom post types and taxonomies? Do you want to list any related content, not just posts?

If you have a big site keep in mind that installing this plugin will create a fulltext index for your posts, so the size of the wp_posts table can almost double.

  1. Upload the plugin to your plugins directory
  2. Enable the plugin
  3. Enable automatic display of related posts

How to place a related content list manually

If you don't enable automatic display of related posts on the plugin options page (the very first setting) you'll have to use the template tag <?php the_related(); ?> to insert a related content list into your theme. If you use this outside of the WordPress loop you have to pass the post ID as parameter.

How to build a custom loop of related posts

Here is a short example how to build your own loop of related posts. This way you can add excerpts, post thumbnails etc.

<?php
    $scores = the_related_get_scores(); // pass the post ID if outside of the loop
    $posts = array_slice( array_keys( $scores ), 0, 5 ); // keep only the the five best results
    $args = array(
        'post__in'            => $posts,
        'posts_per_page'    => 5,
        'caller_get_posts'    => 1 // ignore sticky status
    );
    $my_query = new WP_Query( $args );
    if ( $my_query->have_posts() ) {
        while ( $my_query->have_posts() ) {
            $my_query->the_post();
            echo '<a href="' .  get_permalink( get_the_ID() ) . '">';
            the_title();
            echo '</a>';
            the_excerpt();
            if ( has_post_thumbnail() ) {
                the_post_thumbnail( 'thumb' );
            }
            echo '<br>';
        }
    }
    else {
        echo "No posts found...";
    }
?>

Please notice that the posts won't be ordered by score if you build a loop like this. To accomplish this you'll have to sort the posts in $my_query manually. I have a relevant example on how to sort posts manually on my site. I'll build something like this into the plugin in the future.

How to evaluate different scoring methods

Logged in admins can get the results of all scoring methods by using the template tag <?php the_related_analyze(); ?> in the loop of their theme. Example:

<?php
    if ( current_user_can( 'manage_options' ) && function_exists( 'the_related_analyze' ) ) {
        the_related_analyze();
    }
?>

The scoring methods

Content to content, title to content and title do title are simple MySQL fulltext searches.

Keywords to content and keywords to title do a fulltext search for a string that contains all terms a post has. If a post is in the category Fruits and tagged sweet a fulltext search for "Fruits sweet" will be performed.

Terms against taxonomies searches for posts that have the same terms as the current one. By default only posts that use the same taxonomy are found. It is however as well possible to find posts that use a different taxonomy, by searching this other taxonomy for terms of the same name. See the examples below.

How to use different configurations at the same time

It is possible to use different configurations of the plugin at the same time. Let's say you use the normal related content feature for your posts. But you also have a custom post type 'venue'. To get a custom list of related venues use this:

<?php
the_related(
    get_the_ID(),
    array(
        'usept'    => array(
            'venue'    => true
        ),
        'storage_id'    => 'ventures-better-related-'
    )
);
?>

It is imporant to define a storage ID, or the plugin will overwrite the scores for the standard posts with the scores for your custom configuration. The default storage ID is 'better-related-', so you should avoid that unless you changed the default.

To get a similar listing that includes related posts (from the built-in 'post' post type) use:

<?php
the_related(
    get_the_ID(),
    array(
        'usept'    => array(
            'post'        => true,
            'venue'        => true
        ),
        'maxresults'    => 7,
        'storage_id'    => 'more-better-related-'
    )
);
?>

Now an example that shows off all configuration options available:

<?php
the_related(
    get_the_ID(),
    array(
        'usept'         => array(
            'post'      => true,
            'venue'     => true
        ),
        'usetax'        => array(
            'drinks'    => true,
            'food'      => true,
            'post_tag'  => true,
            'category'  => true
        ),
        'do_c2c'        => 1.5,
        'do_t2c'        => 2,
        'do_k2c'        => 2,
        'do_t2t'        => 1,
        'do_k2t'        => 2,
        'do_x2x'        => 4.3,
        'minscore'      => 25,
        'maxresults'    => 5,
        'log'           => true,
        'loglevel'      => 'taxquery',
        'storage_id'    => 'better-related-full-example-',
        'storage'       => 'transient',
        'cachetime'     => 1,
        'querylimit'    => 10000,
        'incremental'   => true,
        't_querylimit'  => 30000,
        'relatedtitle'  => 'Related venues and posts',
        'relatednone'   => 'No related venues or posts'
    )
);
?>

Notes:

  • Using the usetax parameter means that cross-taxonomy searches will be performed. It is not recommended to use this parameter at the moment, as it's usage will probably change in the future. However, if different taxonomies share terms you can play with this parameter.
  • You should not log on live sites.
  • The high querylimit in this example could slow down your site.
  • The transient storage should only be used temporarily and for testing purposes.
  • Enabling incremental scoring means that the relatedness scores for posts will be calculated in multiple steps. In the example above up to 30000 related posts will be found and the necessary queries will be spread across three page views, assuming there is no cache.

Find posts related to a string

The template tag <?php get_the_related_for_string() ?> returns related posts for a string. The tag <?php the_related_for_string() ?> prints a list of posts related to a string. Usage:

<?php the_related_for_string( 'foo' ); ?>

I use this on 404 pages currently, see the paste for my dynamic WordPress 404 search form. In the future I will probably add a replacement for the built-in WordPress search to this plugin.

mysqld configuration

Generally speaking, the plugin should work out of the box. However, there are two settings you might want to change, see the mysql full-text fine-tuning docs.

ft_min_word_len controls the minumum length of words. This defaults to 4, which might be too small if you use acronyms like CSS, PHP etc. You can change or set that option to 3 in your mysqld config. You will have to re-build your fulltext indexes after doing this, use "REPAIR TABLE table_name QUICK;" where table_name is wp_posts on a default WordPress install.

The second option could be interesting if your site is not in english and you want to improve search results. Use the ft_stopword_file option to create your own stopwords file. MySQL uses english stopwords by default.

Requires: 3.0 or higher
Compatible up to: 3.5.2
Last Updated: 2012-12-13
Downloads: 53,877

Ratings

4 stars
4.5 out of 5 stars

Support

0 of 1 support threads in the last two months have been resolved.

Got something to say? Need help?

Compatibility

+
=
Not enough data

0 people say it works.
0 people say it's broken.

100,1,1
100,3,3 100,1,1 100,2,2 100,1,1 100,1,1 100,1,1 100,4,4
100,1,1
50,2,1
100,1,1
100,1,1
83,6,5
50,2,1
0,1,0
50,2,1
88,8,7
100,2,2
89,9,8
100,3,3
50,2,1
100,1,1
0,1,0
50,4,2
100,1,1
0,1,0
0,1,0