【问题标题】:Get a list of users that are associated with taxonomy获取与分类相关的用户列表
【发布时间】:2021-10-29 13:20:54
【问题描述】:

在 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)

我在网上查了几篇帖子,但关于用户分类关系的帖子很少……所以请帮助我……

非常感谢!!

【问题讨论】:

  • 除了阅读帖子之外,您是否尝试过使用实际文档?帖子通常只关心一两个参数,文档涵盖了所有参数(如果没有,它应该链接源代码,其中参数通常也相对容易发现)
  • 这可以解释为什么它不起作用:get_user_by() 仅支持有限的字段集。分类学不是其中的一部分。但是,是什么阻止您使用查询编写自己的函数,然后为返回的 ID 运行 get_user_by()?
  • 我不确定完整的文档,我不是 php 人。经过一些研究,我遇到了一些使用 'get_objects_in_term' 的帖子,但没能完全理解。 ..我相信它应该将结果从一个查询传递给另一个输入参数..
  • tip: google/search: codex + 感兴趣的函数名。通常会将您带到正确位置的文档。 / 用于参数传递:您的 SQL 查询返回用户 ID,然后使用 get_user_by() 您可以通过用户 ID 获取用户。以防万一这不明显。

标签: php wordpress taxonomy custom-taxonomy


【解决方案1】:

首先,您必须让所有用户使用get_users() 函数。然后迭代用户循环并在此基础上使用authortax_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>";

    } 
}

UPDATE 根据 cmets 中要求的 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 );
}

【讨论】:

  • 非常感谢!它比我想象的要长得多.. 据我所知,我不是一个 php 人(更多的是 sql 人),为什么 'posts_per_page'=> -1 ?这条线是干什么用的?
  • posts_per_page 每页需要多少帖子作为回报。
  • 我只需要获取 user_id (11, 33, 52, 57, 997...) 的列表并返回类似 echo $user_id... 之类的值... 应该在哪里我修改?谢谢你..
  • 很抱歉给您添麻烦了,也许我不清楚... 11,33,52... 只是我需要从 php 中取出的结果的一个示例。我想获取与 taxonomy_id = 1182 相关联的用户的用户 ID 列表。这样,您更新的答案仍然得到该结果?
  • 另外,我有 20K 用户...我从您的代码中看到了循环.. 循环 20k 用户是一种好习惯吗?.. 或者这可能是获得结果的唯一方法,因为有什么限制吗?
猜你喜欢
  • 2020-05-03
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多