Created
May 2, 2018 17:00
-
-
Save froutsis/3ab4adbbdf0b4be3f8940faf35cb3721 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function bit_crp_add_fields( $fields, $post_id ) { | |
global $wpdb; | |
$match_title = strip_tags( get_the_title( $post_id ) ); | |
$match_content = bit_get_the_matcher($post_id); | |
$field_score = ", ( ( MATCH($wpdb->posts.post_title) AGAINST ('%s') *5 ) + ( MATCH($wpdb->postmeta.meta_value) AGAINST ('%s') *10 ) ) as score "; | |
$field_score = $wpdb->prepare( $field_score, $match_title, $match_content ) ; | |
return $fields . $field_score; | |
} | |
add_filter( 'crp_posts_fields', 'bit_crp_add_fields', 10, 2 ); | |
function crp_add_join( $join, $id ) { | |
global $wpdb; | |
return $join . " LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID "; | |
} | |
add_filter( 'crp_posts_join', 'crp_add_join', 10, 2 ); | |
function crp_add_where( $where, $id ) { | |
global $wpdb; | |
return $where . " AND $wpdb->postmeta.meta_key = '_yoast_wpseo_metadesc' "; | |
} | |
add_filter( 'crp_posts_where', 'crp_add_where', 10, 2 ); | |
function bit_crp_add_matcher( $match, $stuff, $post_id ) { | |
global $wpdb; | |
$matcher = " AND ( MATCH($wpdb->posts.post_title) AGAINST ('%s') "; | |
$matcher .= " OR MATCH($wpdb->postmeta.meta_value) AGAINST ('%s') "; | |
$matcher .= " OR MATCH($wpdb->posts.post_title) AGAINST ('%s') ) "; | |
$match_title = strip_tags( get_the_title( $post_id ) ); | |
$match_content = bit_get_the_matcher($post_id); | |
$matcher = $wpdb->prepare( $matcher, $match_title , $match_content, $match_title.' '. $match_content ); | |
return $matcher; | |
} | |
add_filter( 'crp_posts_match', 'bit_crp_add_matcher', 10, 3 ); | |
function bit_crp_filter_orderby( $orderby ) { | |
return 'score DESC'; | |
} | |
add_filter( 'crp_posts_orderby', 'bit_crp_filter_orderby' ); | |
// Function to get the match text | |
function bit_get_the_matcher($post_id){ | |
global $post; | |
if(is_singular()) | |
$bit_post = $post; | |
else | |
$bit_post = get_post($post_id); | |
$bit_content = get_post_meta($post_id , '_yoast_wpseo_metadesc', true); | |
if(empty($bit_content)) $bit_content = get_post_meta($post_id , 'sf_custom_excerpt', true); | |
if(empty($bit_content)) $bit_content = $bit_post->post_content; | |
$bit_content = strip_tags( strip_shortcodes( $bit_content )); | |
return wp_trim_words( $bit_content , 200, '' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment