Plugin / Better Related Posts
Nicolas KuttlerInstallation
Installation
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.
- Upload the plugin to your plugins directory
- Enable the plugin
- 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.
Ratings
Rating breakdown
Details Information
Version
First Released
Total Downloads
Wordpress Version
Tested up to:
Require PHP Version:
Tags
Contributors
Languages
The plugin hasn't been transalated in any language other than English.
DIRECTORY DISCLAIMER
The information provided in this THEME/PLUGIN DIRECTORY is made available for information purposes only, and intended to serve as a resource to enable visitors to select a relevant theme or plugin. wpSocket gives no warranty of any kind, express or implied with regard to the information, including without limitation any warranty that the particular theme or plugin that you select is qualified on your situation.
The information in the individual theme or plugin displayed in the Directory is provided by the owners and contributors themselves. wpSocket gives no warranty as to the accuracy of the information and will not be liable to you for any loss or damage suffered by you as a consequence of your reliance on the information.
Links to respective sites are offered to assist in accessing additional information. The links may be outdated or broken. Connect to outside sites at your own risk. The Theme/Plugin Directory does not endorse the content or accuracy of any listing or external website.
While information is made available, no guarantee is given that the details provided are correct, complete or up-to-date.
wpSocket is not related to the theme or plugin, and also not responsible and expressly disclaims all liability for, damages of any kind, arising out of the use, reference to, or reliance on, any information or business listed throughout our site.