【问题标题】:Update and Add tags post on save post using wp_update_post使用 wp_update_post 在保存帖子上更新和添加标签帖子
【发布时间】:2012-09-04 16:10:01
【问题描述】:

我正在尝试构建一个函数来保存帖子 wp_update_post 以添加用逗号分隔的标签,如代码中所示

//Create the post array
$post = array(
'ID' => 5,
'tags_input' => 'foo,bar,baz');     

// Update the post
wp_update_post($post);

代码在我的 function.php 主题中运行良好,但是我想启动一个函数,使其仅在帖子或使用add_filter ('wp_update_post','');save_post('wp_update_post,''); 创建新帖子的版本中运行

我试过这样做

function add_tags($post) {
    global $post;
    $idpost = $post->ID;
    $tags = 'tag1, tag2, tag3, tag4';

    $post = array(
    'ID' => $idpost,
    'tags_input' => $tags);     

    wp_update_post($post);

    return $post;
}
add_filter( 'wp_update_post', 'add_tags');
save_post( 'wp_update_post', 'add_tags');

放入与不运行之间的无限循环

我做错了什么

【问题讨论】:

    标签: php wordpress tags


    【解决方案1】:

    已解决:

    function my_function(){
        if ( ! wp_is_post_revision( $post_id ) ){
    
            // unhook this function so it doesn't loop infinitely
            remove_action('save_post', 'my_function');
    
            // update the post, which calls save_post again
            $post = array(
                    'ID' => 5,
                    'tags_input' => 'foo,bar,baz');  
            wp_update_post( $post );
    
            // re-hook this function
            add_action('save_post', 'my_function');
        }
    }
    // hook the function
    add_action('save_post', 'my_function');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 2013-12-03
      相关资源
      最近更新 更多