Skip to content

Instantly share code, notes, and snippets.

@amyinorbit
Last active January 2, 2016 14:19
Show Gist options
  • Save amyinorbit/8316055 to your computer and use it in GitHub Desktop.
Save amyinorbit/8316055 to your computer and use it in GitHub Desktop.
Crée un fichier search.json à la racine d'un blog wordpress
add_action( 'transition_post_status', 'a_new_post', 10, 3 );
function a_new_post( $new_status, $old_status, $post )
{
if ( 'publish' !== $new_status or 'publish' === $old_status )
{
return;
}
$postsArray = Array(); // un tableau vide
$args = array('post_type' => 'post','posts_per_page' => -1);
$post_query = new WP_Query($args);
if($post_query->have_posts()) {
while($post_query->have_posts()) {
$post_query->the_post();
$currentPost = Array(); // un tableau vide pour l'article en cours
$currentPost["title"] = get_the_title(); // on ajoute le titre
$currentPost["url"] = get_permalink(); // l'url
array_push($postsArray, $currentPost); // et on ajoute le tableau de l'article au tableau global
}
$json = json_encode($postsArray); // on encode tout ça en JSON
if(!file_put_contents(ABSPATH."/search.json", $json)) { // on ecrit tout ca dans un fichier
throw new Exception("Probleme lors de l'ecrtiture du fichier");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment