Last active
January 2, 2016 14:19
-
-
Save amyinorbit/8316055 to your computer and use it in GitHub Desktop.
Crée un fichier search.json à la racine d'un blog wordpress
This file contains hidden or 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
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