Skip to content

Instantly share code, notes, and snippets.

@ajskelton
Last active January 31, 2024 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajskelton/7ab9dacf39eda75aee5e06c2e40d7683 to your computer and use it in GitHub Desktop.
Save ajskelton/7ab9dacf39eda75aee5e06c2e40d7683 to your computer and use it in GitHub Desktop.
Add 'searchwp_related_sample_engines' filter to the get_samples function
/**
* Get associative array of sample results for keywords
*
* @param string $existing_keywords
*
* @param int $post_id
*
* @return array
*/
public function get_samples( $existing_keywords = '', $post_id = 0 ) {
$samples = array();
if ( function_exists( 'SWP' ) ) {
$engines = SWP()->settings['engines'];
} else if ( class_exists( '\\SearchWP\\Settings' ) ) {
$engines = \SearchWP\Settings::get_engines();
}
if ( empty( $engines ) ) {
return $samples;
}
$engines = apply_filters( 'searchwp_related_sample_engines', $engines, $post_id );
foreach ( $engines as $engine => $engine_settings ) {
$post_data = $this->related->get( array(
'engine' => $engine,
's' => $existing_keywords,
'post__not_in' => array( $post_id ),
), $post_id );
if ( ! empty( $post_data ) ) {
foreach ( $post_data as $key => $val ) {
$val = absint( $val );
$post_data[ $key ] = array(
'ID' => $val,
'post_title' => get_the_title( $val ),
'permalink' => get_permalink( $val ),
'post_type' => get_post_type( $val ),
);
}
}
if ( function_exists( 'SWP' ) ) {
$label = isset( $engine_settings['searchwp_engine_label'] ) ? esc_html( $engine_settings['searchwp_engine_label'] ) : esc_html__( 'Default', 'searchwp-related' );
} else {
$label = $engine_settings->get_label();
}
$samples[] = array(
'engine' => array(
'name' => $engine,
'label' => $label,
),
'samples' => empty( $post_data ) ? array() : $post_data,
);
}
return $samples;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment