WP发布更新后向表中添加数据

时间:2016-08-18 09:26:57

标签: php wordpress

我试图将帖子和搜索字词保存到wordpress元文章帖子中保存的特定表格中。元数据将单词保存/显示为后期元数据(正在工作!),但不会将帖子和单词保存到“搜索特殊情况”中。表。

我做错了什么?

Metabox将单词保存为post meta_data:

function specialisaties_post_meta_boxes() {

runScriptGetSearchTerms();
    runScriptInsertSearchterms();

    global $wpdb;
        $table_specaility = $wpdb->prefix . "search_specialties";

        $result = $wpdb->get_results("SELECT * from {$table_specaility} ");
        $specials=array();
        foreach($result as $row){
        $specials[]=$row->speciality;
        }
    $meta_boxes = array(
        'type' => array( 'name' => 'type', 'title' => 'Soort Werk:',  'type' => 'select', 'options'=>array('work1','work2') ),
        'zoekwoorden' => array( 'name' => 'zoekwoorden', 'title' => 'Zoekwoorden:',  'type' => 'text',),
        );
    return apply_filters( 'hybrid_post_meta_boxes', $meta_boxes );
}

在同一个文件中删除当前字符串的脚本:

function runScriptGetSearchTerms()
{
    global $wpdb;
    //Delete all words before the INSERT query
    $deleteQuery = "DELETE from search_suggest_specialties";
    $wpdb->query($deleteQuery);

    $allWordsString;
    $allWordsArray;

    $query = "  SELECT SearchTerms
                FROM  `search_specialties`";

    $result = $wpdb->get_results($query);

    //Make one string of all the searchTerms
    foreach($result as $terms)
    {
        $allWordsString.= $terms->SearchTerms .",";
    }

    $insertQuery = "INSERT INTO search_suggest_specialties (word) VALUES ";
    //Split the string. Every word seperated by a comma wil be split an stored in an array
    $allWordsArray = explode(",", $allWordsString);

    //append all words to the INSERT query
    foreach($allWordsArray as $word)
    {
        $insertQuery.="('".trim($word)."'), ";
    }
    //Remove the last two characters, an empty space and the las comma
    $insertQuery = substr($insertQuery, 0, -2);
    $wpdb->query($insertQuery);

}

在同一文件中编写脚本,将搜索词插入表中。

function runScriptInsertSearchterms()
{
    global $wpdb;
    //Delete all words before the INSERT query
    $deleteQuery = "DELETE from search_specialties_searchterms";
    $wpdb->query($deleteQuery);

    $allWordsString;
    $allWordsArray;
    $insertQuery = "INSERT INTO search_specialties_searchterms (specialtyId, words) VALUES";

    $query = "  SELECT Name, ForeignId, SearchTerms FROM `search_specialties`";

    $result = $wpdb->get_results($query);

    //Make one string of all the searchTerms
    foreach($result as $terms)
    {
        $allWordsArray = explode(",", $terms->SearchTerms);
        foreach($allWordsArray as $word)
        {
            $insertQuery.= " ('".$terms->ForeignId."', '".trim($word)."'), ";
        }
            $insertQuery.= " ('".$terms->ForeignId."', '".trim($terms->Name)."'), ";
    }
    $insertQuery = substr($insertQuery, 0, -2);
    $wpdb->query($insertQuery );
}

0 个答案:

没有答案