最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Associate users with Custom Post Type and list associated users

programmeradmin2浏览0评论

I have created a Custom Post Type called Program

I want logged in users to get associated with a program:

  1. User goes to Program page
  2. User clicks on Register button
  3. User fills details in the form and pushes Submit button.

Afterwards I should be able to create a report which lists:

  1. All the users who are associated with any Program
  2. List of users who are associated with a selected program & role

How should I do that?

I have created a Custom Post Type called Program

I want logged in users to get associated with a program:

  1. User goes to Program page
  2. User clicks on Register button
  3. User fills details in the form and pushes Submit button.

Afterwards I should be able to create a report which lists:

  1. All the users who are associated with any Program
  2. List of users who are associated with a selected program & role

How should I do that?

Share Improve this question edited Mar 24, 2020 at 0:05 WordPress Speed 2,2833 gold badges19 silver badges34 bronze badges asked Mar 22, 2020 at 8:34 Khirat HussainKhirat Hussain 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You have to create a custom taxonomy:

add_action( 'init', function(){
    register_taxonomy( 'program', 'your_post_type', array() );
});

To get users associated with a program, get all posts that have your desired program term:

$query = new WP_Query( array(
    'post_type' => 'your_post_type',
    'tax_query' => array(
        array(
            'taxonomy' => 'program',
            'field'    => 'name',
            'terms'    => 'your_program_name',
        ),
    ),
) );

Now get all the result posts authors and that would give you the users associated to that specific program.

$posts = $query->posts;

foreach($posts as $post) {
    $author = $post->post_author;
}
发布评论

评论列表(0)

  1. 暂无评论