2016-09-01 9 views
2

WordPressには、タクソノミー用語の文字列が投稿のタイトルと等しい場合、パーマリンクルールがこの場合失敗するため、スラッグを更新するという要件があります。ワードプレスwp_update_postはポスト名を更新していません

私のコードは次のとおりです。私は、ログを取得しています必要なポストに

add_action('save_post', 'change_default_slug', 10,2); 
function change_default_slug($post_id, $post) { 
    error_log($post_id); 
    error_log($post->post_title); 
    error_log($post->post_name); 

    $taxonomies=get_taxonomies('','names'); 
    $terms = wp_get_post_terms($post->ID, $taxonomies, array("fields" => "names")); 
    $terms = array_map('strtolower', $terms); 

    error_log('terms :' . json_encode($terms)); 

    $title = strtolower($post->post_title); 
    error_log('title : ' . $title); 

    if(in_array($title, $terms)) { 

    error_log('yessss'); 

    $args = array (
     'ID'  => $post->ID, 
     'post_name' => $post->post_name . "-post" 
    ); 
    $result = wp_update_post($update_args); 
    error_log('result'); 
    error_log(json_encode($result)); 
    } else { 
    error_log('noooooooo'); 

    } 
} 

:YESSS結果0スラッグが更新されていません 。 同じようにお手伝いください。私は文字通りこの問題で利用可能なすべてのソリューションを試しました。それは私が使用して最終的にそれを解決することができたのfunctions.php

+0

$結果= wp_update_post($ update_argsを)。 $ result = wp_update_post(args)にする必要があります。 – Juergen

答えて

0

を経由して行う必要があります。wp_insert_post()

$taxonomies=get_taxonomies('','names'); 
    $terms = wp_get_post_terms($post->ID, $taxonomies, array("fields" => "all")); 

    foreach($terms as $term) { 

     if($term->taxonomy == 'category'){ 
      $categories[] = $term->term_id; 
     } else if($term->taxonomy == 'post_tag'){ 
      $tags[] = $term->term_id; 
     } 
    } 
    . 
    . 
    . 
    //detach the hook to avoid infinite looping of the hook on post insert 
    remove_action('save_post', 'change_default_slug', 10,2); 

    //insert post 
    $result = wp_insert_post($post, true); 

    //attach post tags to the current post (since not by default attached) 
    wp_set_post_terms($post_id,$tags,'post_tag'); 

    //attach post categories to the current post (since not by default attached) 
    wp_set_post_terms($post_id,$categories,'category'); 

    //re-activate the hook 
    add_action('save_post', 'change_default_slug', 10,2); 
関連する問題