サポート » 使い方全般 » 記事の保存時、投稿内容に特定の文字列があった場合に指定したタグを追加したい

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • グローバルの $post ではなく、アクション save_post へ渡される投稿 ID を使ってはいかがですか。
    下記ページ(英語ですが)に載っている一番目の例が参考になると思います。
    Plugin API/Action Reference/save post
    https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

    トピック投稿者 lady122

    (@luv5ick)

    @gblsm さん

    以下のように直したら期待通りの動きになってくれました!
    ご教示ありがとうございました。

    function xxx_set_tags($post_id){
      $content_post = get_post($post_id);
      $content = $content_post->post_content;
      if(strpos($content, '★特定の文字列★') !== false){
        wp_add_post_tags($post_id, '★指定したタグ★');
      }
    }
    add_action('save_post', 'mtc_set_tags');
    トピック投稿者 lady122

    (@luv5ick)

    関数名がズレてたので追記しておきます。

    function xxx_set_tags($post_id){
      $content_post = get_post($post_id);
      $content = $content_post->post_content;
      if(strpos($content, '★特定の文字列★') !== false){
        wp_add_post_tags($post_id, '★指定したタグ★');
      }
    }
    add_action('save_post', 'xxx_set_tags');
3件の返信を表示中 - 1 - 3件目 (全3件中)
  • トピック「記事の保存時、投稿内容に特定の文字列があった場合に指定したタグを追加したい」には新たに返信することはできません。