Add a new page for every value added in my db table

Question

i’m working on plugin and i need create a page every time a new values is insert in my table.

That s my function for create a page with value name. In $name there are all the values from my db and in my $namepage all slug.

$query = 'SELECT id_secteurs, libelle FROM wp_secteurs';
$query2 = 'SELECT post_name FROM wp_posts';
$reponse = $base->query($query);
$reponse2 = $base->query($query2);
$namepages = array();
$name = array();
$namepost = array();
while ($donnees = $reponse->fetch()) {
    if (!in_array($donnees['libelle'], $namepages, true)) {
        array_push($namepages, strtolower($donnees['libelle']) . "-s" . $donnees['id_secteurs']);
        array_push($name, $donnees['libelle']);
    }
}
while ($donnees = $reponse2->fetch()) {
    if (!in_array($donnees['post_name'], $namepost, true)) {
        array_push($namepost, $donnees['post_name']);
    }
}

function mic_create_new_page($name, $namepages) {
    global $user_ID;
    if (post_exists($namepages) == false) {
        $new_post = array(
            'post_title' => 'Services ' . $name,
            'post_content' => 'New post content',
            'post_status' => 'publish',
            'post_date' => date('Y-m-d H:i:s'),
            'post_author' => $user_ID,
            'post_type' => 'page',
            'post_name' => $namepages
        );
        $post_id = wp_insert_post($new_post);
    }
}
register_activation_hook(__FILE__, 'mic_create_new_page');

For fix the problem at every refresh the same post was added:

function zed93_create_new_page() {
    for ($i = 0; $i < count($namepages); $i++) {
        mic_create_new_page($name[$i],$namepages[$i]);    
    }
}
register_activation_hook(__FILE__, 'zed93_create_new_page');

But now, how can i create a page programmatically for every new value?

0
, , , , Zed93 2 years 2020-07-29T05:10:21-05:00 0 Answers 47 views 0

Leave an answer

Browse
Browse