Skip Navigation

[Resolved] Create additional author field in posts

This thread is resolved. Here is a description of the problem and solution.

Problem:
The issue here is that the user wanted to autopopulate a select field with their users who are editors.

Solution:

Add the following to your functions.php file, Replacing Team Leader with the name of your select field.

add_filter( 'wpt_field_options', 'populate_select', 10, 3);
function populate_select( $options, $title, $type ){
    switch( $title ){
        case 'Team leader':
            $options = array();
            $args = array(
                                         'role'         => 'some-role',
                                );
            $posts_array = get_users( $args );
            foreach ($users_array as $user) {
       
                $options[] = array(
                    '#value' => $user->ID,
                    '#title' => $user->display_name,
                     );
                }
                
                break;
}
    return $options;
}

Also you will need to replace the text some-role with the slug of the role of the users that you want to retrieve.

This support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 2 voices.

Last updated by hannahM 6 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#612704

Hi there,

I am trying to create a secondary 'author' field on posts on Bristol Live Magazine: hidden link

The reason we are doing this is because we want to be able to select a photographer for live reviews, and have the photographer's name and a link to their profile display on the post.

Once the field is created I will be able to add it to the post templates myself, but I'm unsure how to create the field.

What I want is for it to operate in the same way as the 'author' field, so it shows a dropdown list of site users which the editor can select from.

Is this something I'm able to do within Toolset?

Many thanks

Hannah

#612919

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hannah,

Thank you for contacting our support forum.

I'm not too sure how well the instructions below will work with our Toolset plugins and retrieving the author information but you can have a look and let me know if it helps.

hidden link

Thanks,
Shane

#614176

Hi Shane,

Thanks for this. I had found that article before posting here. It just links to a couple of plugins which unfortunately don't provide what I'm after here.

What I'm looking to do is to create a separate, secondary author field which can be used when creating a post, so I can differentiate between Author Field 1 and Author Field 2 on the front-end of the site.

I guess what I'm asking is whether it's possible to create a drop-down post field with Types and then auto-populate it from the site users?

Thanks

Hannah

#614422

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hannah,

You can create a secondary field.

Is it that you want to manually assign the secondary author by selecting them from a list ? If so then this list will need to be manually populated with the list of the users.

Some custom code will need to be used in order to dynamically generate the list.

Thanks,
Shane

#620418

Hi Shane,

Yes, that's exactly what I want to be able to do - I want to be able to select a secondary author for the post from the list of users on the site; exactly as you would with the primary author field.

Are you able to point me towards any documentation on how to dynamically generate the list?

Thanks
Hannah

#620559

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hannah,

Unfortunately we don't have any documentation on this as its not possible to auto populate without custom code.

However you can try using the code below and customizing it to get the users.

To Get this to work you will need to replace the Text “Team Leader” with the name of the select field you wish to populate. Everything else is a Basic wordpress Query

add_filter( 'wpt_field_options', 'populate_select', 10, 3);
function populate_select( $options, $title, $type ){
    switch( $title ){
        case 'Team leader':
            $options = array();
			$args = array(
                                         'role'         => 'some-role',
								);
            $posts_array = get_users( $args );
            foreach ($users_array as $user) {
      
                $options[] = array(
                    '#value' => $user->ID,
                    '#title' => $user->display_name,
                     );
                }
               
                break;
}
    return $options;
}

Please try this and let me know if it helps.

Thanks,
Shane

#623206

Hi Shane,

That's great, many thanks for your help - I'll give this a try and see if I can make it work 🙂

Ta!

Hannah