Last active
May 17, 2021 14:40
-
-
Save alloyking/9762126e9b821c6f484ff18e1262c977 to your computer and use it in GitHub Desktop.
This file contains 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
//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