• Resolved sultanbekov

    (@sultanbekov)


    Hello! First of all thanks for building this and making it available publicly! I have found your Query class very helpful as I tweaked the query and put the output directly into my php.

    I am wondering if there is a way to remove the title from being considered in the matching algorithm? All of my posts start with “can dogs eat [foodname]” and so I’m seeing about 4/6 Related Posts being the same on each page. I would like less repetition of the same posts.

    You can see the plugin at the bottom of my page: https://www.candogseatthat.com/vegetables/can-dogs-eat-radishes/

    Thanks in advance,

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Ajay

    (@ajay)

    Please see this filter: https://github.com/WebberZone/contextual-related-posts/blob/v3.3.1/includes/class-crp-query.php#L473

    You can override the match_fields to only use the content.

    Thread Starter sultanbekov

    (@sultanbekov)

    Thanks for the quick response! Can you please give a code example of how I might override the match_fields from the theme level?

    Plugin Author Ajay

    (@ajay)

    function change_crp_match_fields( $match_fields ) {
    	global $wpdb;
    
    	// Are we matching only the title or the post content as well?
    	$match_fields = array(
    		"$wpdb->posts.post_content",
    	);
    
    	return $match_fields;
    }
    add_filter( 'crp_posts_match_fields', 'change_crp_match_fields' );
    
    Thread Starter sultanbekov

    (@sultanbekov)

    thanks for showing me how to use the filter. That is working for me! One thing to note for anyone else who may be doing this: when I added the above filter, I kept getting a DB error:

    Can't find FULLTEXT index matching the column list

    My query looked like this:

    SELECT wp_posts.*, MATCH (wp_posts.post_content) AGAINST ('Can Dogs Eat Stri…') as score
    FROM wp_posts
    INNER JOIN wp_postmeta
    ON ( wp_posts.ID = wp_postmeta.post_id )
    WHERE 1=1
    AND ( wp_posts.post_date <= '2023-02-17 02:32:43' )
    AND wp_posts.ID NOT IN (642)
    AND ( ( wp_postmeta.meta_key = 'cdet_safety_classification'
    AND wp_postmeta.meta_value = 'safe' ) )
    AND wp_posts.post_type = 'post'
    AND ((wp_posts.post_status = 'publish'
    OR wp_posts.post_status = 'inherit'))
    AND MATCH (wp_posts.post_content) AGAINST ('Can Dogs Eat Stri…')
    GROUP BY wp_posts.ID
    ORDER BY score DESC
    LIMIT 0, 6

    With some trial and error, I ended up running this sql and it fixed the error:

    ALTER TABLE wp_posts ADD FULLTEXT crp_related_content(post_content);

    The matching is working better for me now that I only match for the post_content. Thx again!

    Plugin Author Ajay

    (@ajay)

    Of course! I had removed this when I updated the plugin to use either the post title or both.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Ignore post title in the matching algorithm’ is closed to new replies.