在 WP 中,我想獲取與分類法關聯的用戶 ID 串列。我知道 SQL 查詢,但我不確定如何構建一行 php 代碼來獲得相同的結果(類似于 $users = get_user_by(parameters))?
SELECT u.ID
FROM wp_users u
INNER JOIN wp_term_relationships r
ON u.ID = r.object_id
WHERE u.user_status = 0
AND r.term_taxonomy_id = 1186
把它變成這樣的東西
$users = get_user_by(parameters) or
get_objects_in_term(parameters)
我在網上查了一些帖子,但很少有關于用戶分類關系的帖子......所以請幫助我......
非常感謝!!
uj5u.com熱心網友回復:
首先,您必須讓所有用戶都使用該get_users()功能。然后迭代用戶回圈并基于該回圈使用author和獲取用戶帖子tax_query。試試下面的代碼。
$user_args = array( 'orderby' => 'ID' );
$users = get_users($user_args);
//Loop through each peche author
foreach($users as $user){
$user_posts = new WP_Query(array(
'post_type' => 'product', // your custom post type
'author' => $user->ID,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat', // your custom taxonomy slug
'field' => 'term_id',
'terms' => 22 // your term id.
)
)
));
if( $user_posts->have_posts() ) {
echo $user->first_name . ' ' . $user->last_name. ' associated with taxonomy 22'."</br>";
}
}
根據 OP 請求更新
$user_args = array(
'include' => array(11, 33, 52, 57, 997) // add your user ids here by comma seprate.
);
$users = get_users( $user_args );
//Loop through each peche author
foreach($users as $user){
$user_posts = new WP_Query(array(
'post_type' => 'product', // your custom post type
'author' => $user->ID,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat', // your custom taxonomy slug
'field' => 'term_id',
'terms' => 22 // your term id.
)
)
));
if( $user_posts->have_posts() ) {
echo $user->ID."</br>";
}
}
根據評論中要求的 OP更新。
$all_ids = array();
$result = $wpdb->get_results( "SELECT u.ID FROM wp_users u INNER JOIN wp_term_relationships r ON u.ID = r.object_id WHERE u.user_status = 0 AND r.term_taxonomy_id = 1186", ARRAY_A );
foreach ( $result as $key => $id ) {
$all_ids[] = $id['ID'];
}
if( !empty( $all_ids ) ){
echo implode( ',', $all_ids );
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/342805.html
標籤:php WordPress的 分类 自定义分类法
