0

我正在尝试在发布时插入帖子 ID 到自定义表,但它没有添加正确的 ID,但这里 0 是我的函数

//get post id on publish
function get_publishing_id($post_id) {
  $post= get_post($post_id);
  if ($post->post_type == 'post'
      && $post->post_status == 'publish') {

        // insert data on publish
        global $wpdb;
        $wpdb->insert( 
            $wpdb->prefix.'banner_views', 
            array( 
                'postid' => $post,
                'view_count' => 12,
            )
        );

  } // end if
} // end function
add_action('save_post','get_publishing_id');

请帮助解决这个问题。非常感谢...

4

1 回答 1

0

解决了。

错误是没有像 $post->ID 那样使用 $post 传递 ID

//get post id on publish
function get_publishing_id($post_id) {
  $post= get_post($post_id);
  if ($post->post_type == 'post'
      && $post->post_status == 'publish') {

        // insert data on publish
        global $wpdb;
        $wpdb->insert( 
            $wpdb->prefix.'banner_views', 
            array( 
                'postid' => $post->ID,// here was the mistake
                'view_count' => 12,
            )
        );

  } // end if
} // end function
add_action('save_post','get_publishing_id');

所以上面的代码对我有用。

非常感谢

于 2012-10-02T11:33:16.820 回答