Select Page

我們可以用 php 程式,來取得使用者的資訊,最重要的當然是 User ID 的取得,能取得 User ID 才能進行使用者的資訊取得,取得uid的方法百百種,這邊列出四種常用的場景,取得當下登入的使用者ID,另用會員的關鍵資料取得會員ID,利用文章取得作者ID,在商店中取得客戶ID,

1.get_current_user_id()

$current_user_id = get_current_user_id();
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;

2.使用會員資訊取得ID,get_user_by()

利用Email取的ID

$the_user = get_user_by('email', '[email protected]');
$the_user_id = $the_user->ID;

利用登入帳號

$the_user = get_user_by('login', 'rain');
$the_user_id = $the_user->ID;

3.利用文章資訊取得作者 ID

$my_post = get_post( $id ); // $id - Post ID
echo $my_post->post_author; // print post author ID

4.在woocommerce中取得訂單的客戶ID

$order = wc_get_order( 123 ); // your order ID
$customer_id = $order->get_customer_id(); // or $order->get_user_id() – the same