Skip to content

Instantly share code, notes, and snippets.

@soderlind
Created July 11, 2018 23:25
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 soderlind/1426b552431e399e980d322175bc8c65 to your computer and use it in GitHub Desktop.
Save soderlind/1426b552431e399e980d322175bc8c65 to your computer and use it in GitHub Desktop.
On SearchWP activation, set the defaults for my custom post types.
<?php
namespace Soderlind\Demo\SearchWP;
add_filter( 'searchwp_custom_field_keys', __NAMESPACE__ . '\\on_searchwp_custom_field_keys' );
add_filter( 'searchwp_initial_engine_settings', __NAMESPACE__ . '\\on_searchwp_initial_engine_settings' );
/**
* Add custom fields to the search index
*
* @param array $keys
* @return array
*/
function on_searchwp_custom_field_keys( $keys ) {
$keys[] = 'ingress';
$keys[] = 'hogan_%'; // % = wildcard. I have multiple custom post types prefixed hogan_
return $keys;
}
/**
* On SearchWP activation, set the defaults for my custom post types.
*
* @param array $settings All of the default engine settings.
* @return array
*/
function on_searchwp_initial_engine_settings( $settings ) {
$post_types = array( 'post', 'page' );
foreach ( $post_types as $post_type ) {
$settings['default'][ $post_type ]['weights']['cf'][ uniqid( 'nettsteder' ) ] = array(
'metakey' => 'ingress',
'weight' => 40,
);
$settings['default'][ $post_type ]['weights']['cf'][ uniqid( 'nettsteder' ) ] = array(
'metakey' => 'hogan_%',
'weight' => 10,
);
}
return $settings;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment