I know that there are many plugins which does this. but I am wondering why all plugins only get related post by tags and not by title?
what is an efficient way to r开发者_运维百科etrieve all similar post only by title in the single.php. for example, if I have two posts whose title are "speed up wordpress loading time" and "speed up wordpress backend". then in one post, the other one should be related and shown.
I can think of a way to do that, which is to get all posts under the same category and using similar_text() to compare each title against the current one. then order the result and show the top ones from the list. is this a good and effecient way to do that?
any suggestions? please attach a snippet of code if you have a solution.
I'd suggest you don't load related posts on page load because this will dramatically hurt your website's performance.
The free WordPress plugin Related Posts for WordPress automatically finds related posts (amongst others based on title) and caches them for you, offering you real related posts without hurting your website's performance. After the automatic linking is done you can, if required, manually add, edit or delete related posts.
You can give it a try via the WordPress.org repo: http://wordpress.org/plugins/related-posts-for-wp/
It involves a bit more work, but you could add a fulltext index to the title column of the posts table and then use MATCH AGAINST
in your query to retrieve similar posts ordered by relevance.
To add a fulltext index:
CREATE INDEX fulltext2 ON wp_posts(post_title(255));
To make a query using MATCH AGAINST:
SELECT ID, post_title,
MATCH(post_title) AGAINST ('$post_title') AS Similarity
FROM wp_posts
WHERE MATCH(post_title) AGAINST('$post_title')
ORDER BY Similarity DESC;
Note: I am not specially fluent in SQL, if you decide for this option perhaps someone could improve the SQL above.
精彩评论