• Hi,

    The built-in “Related Posts” algorithm doesn’t seems accurate enough to show relevant posts.

    Is it possible to modify its parameters so that it only considers post titles, tags, categories, etc. (eg: from posts made in last 1 year) to show best related posts.

    I know I can alternatively use a 3rd party plugin but I want to know if we can customize the one in Hueman theme or not?

    Thanks in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi mayurjango. The related posts function “function alx_related_posts()” is in the parent theme functions.php file. You could copy that function (without the “if(!function_exists…” line or the trailing closing bracket “}” line) to your child theme and make your adjustments there. Also, you can set the posts to be related by categories or tags in Theme Options > Blog > Single – Related Posts.

    Thread Starter mayurjango

    (@mayurjango)

    Hi bdbrown,

    I guess that would be pretty complicated for me. Is it possible to set the posts to be related by titles as well as tags and/or categories?

    I believe you’d need to modify the alx_related_posts function to accomplish that.

    Thread Starter mayurjango

    (@mayurjango)

    Hi bdbrown,

    What should I modify or add in below code from functions.php to show related posts by titles as well as tags and/or categories?

    /*  Related posts
    /* ------------------------------------ */
    if ( ! function_exists( 'alx_related_posts' ) ) {
    
    	function alx_related_posts() {
    		wp_reset_postdata();
    		global $post;
    
    		// Define shared post arguments
    		$args = array(
    			'no_found_rows'				=> true,
    			'update_post_meta_cache'	=> false,
    			'update_post_term_cache'	=> false,
    			'ignore_sticky_posts'		=> 1,
    			'orderby'					=> 'rand',
    			'post__not_in'				=> array($post->ID),
    			'posts_per_page'			=> 3
    		);
    		// Related by categories
    		if ( ot_get_option('related-posts') == 'categories' ) {
    
    			$cats = get_post_meta($post->ID, 'related-cat', true);
    
    			if ( !$cats ) {
    				$cats = wp_get_post_categories($post->ID, array('fields'=>'ids'));
    				$args['category__in'] = $cats;
    			} else {
    				$args['cat'] = $cats;
    			}
    		}
    		// Related by tags
    		if ( ot_get_option('related-posts') == 'tags' ) {
    
    			$tags = get_post_meta($post->ID, 'related-tag', true);
    
    			if ( !$tags ) {
    				$tags = wp_get_post_tags($post->ID, array('fields'=>'ids'));
    				$args['tag__in'] = $tags;
    			} else {
    				$args['tag_slug__in'] = explode(',', $tags);
    			}
    			if ( !$tags ) { $break = true; }
    		}
    
    		$query = !isset($break)?new WP_Query($args):new WP_Query;
    		return $query;
    	}
    
    }

    Please help.

    I reviewed several approaches that use functions, filters and/or SQL queries to get related posts by title. It can be done with code but I think it would take some work to get it right. Searching the database for text strings usually has a higher overhead and, if not optimized, could affect page speed, especially when the search is performed each time a page is served. If you want to base related posts on post title, my suggestion would be to disable the built-in related posts option and use a plugin since the research and development work has already been done and tested. Here are three that might do the trick:
    Yet Another Related Posts Plugin (YARPP)
    Related Posts for WordPress
    Contextual Related Posts

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How can I customize Related posts algorithm to show appropriate results’ is closed to new replies.