Skip to content

Instantly share code, notes, and snippets.

@alloyking
Last active May 17, 2021 14:40
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 alloyking/9762126e9b821c6f484ff18e1262c977 to your computer and use it in GitHub Desktop.
Save alloyking/9762126e9b821c6f484ff18e1262c977 to your computer and use it in GitHub Desktop.
//include( plugin_dir_path( __FILE__ ) . 'vendor/autoload.php');
//use MeiliSearch\Client;
// ^ in your class
public function buildIndex(){
$client = new Client('http://127.0.0.1:7700');
$args = array(
'posts_per_page' => -1,
'post_status' => 'publish',
'post_type' => 'event'
);
$posts = new WP_Query($args);
$count = $posts->found_posts;
$posts = $posts->get_posts();
# An index is where the documents are stored.
$index = $client->index('event');
$allPosts = [];
foreach( $this->genLoop($posts) as $post ) {
$postArray = (array) $post;
$postMeta = get_post_meta($post->ID);
foreach ($postMeta as $key => $value) {
if(is_serialized(current($value))){
$postArray[$key] = unserialize(current($value));
} else {
if (DateTime::createFromFormat('Y-m-d H:i:s',current($value)) !== false) {
$postArray[$key] = (int) strtotime(current($value));
} else {
$postArray[$key] = current($value);
}
}
}
$allPosts[] = $postArray;
}
$add = $index->addDocuments($allPosts);
}
//generator
protected function genLoop($posts){
foreach ($posts as $post) {
yield $post;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment