【问题标题】:WordPress set post terms and - add action - save post issueWordPress 设置帖子条款并 - 添加操作 - 保存帖子问题
【发布时间】:2015-03-09 20:46:12
【问题描述】:

尝试通过添加操作(保存帖子)挂钩为帖子设置自定义分类术语

这是我的代码

function dosomething(){

global $post, $wpdb;
$id = $post->ID;
global $id; // using id somewhere else 
$cat_ids = array( 45,35 ); // will be vars at somepoint in the future 

wp_set_post_terms( $id , $cat_ids, 'OriginalTag'); 
}

add_action('save_post', 'dosomething',10); // this will run the function on page load 

这对我不起作用?但是通过反复试验我发现,如果我将 set_post_terms 函数中的 $id 替换为帖子的实际值,如下所示:

wp_set_post_terms( 2154 , $cat_ids, 'OriginalTag'); //2154 being the post ID

一切正常......:/无法弄清楚我在这里做错了什么

附言我已经回显了 $id 并且它确实返回了正确的值

【问题讨论】:

    标签: wordpress function variables


    【解决方案1】:
    function dosomething($id){
        $cat_ids = array( 290, 44 ); // will be vars at somepoint in the future
        wp_set_post_terms( $id , $cat_ids, 'post_tag');
    }
    
    add_action('save_post', 'dosomething', 10, 1); // this will run the function on page load
    

    这对我有用,显然我已经替换了我自己的标签 ID 并使用默认分类法,所以将你自己的分类法重新放入。我通过操作调用传递新帖子的 $id,而不是全局变量这对我也不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多