Skip Navigation

[Resolved] Multiple authors per post

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

Problem:
Can we add multiple authors per post using Toolset?

Solution:
Not natively, but with some custom code and a few additional fields, it is possible to achieve similar results.
You can see here a tutorial-like thread where the goal is discussed and resolved with detailed instructions.
https://toolset.com/forums/topic/multiple-authors-per-post-2/#post-1489311

We can't code such solutions for you but with the provided example, it should be possible to easily adapt this to any install.
If further help is required for this sort of custom code, then https://toolset.com/contractors/ might as well be an option to consult.

This support ticket is created 4 years, 11 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 11 replies, has 2 voices.

Last updated by Rune Brynestad 4 years, 11 months ago.

Assisted by: Beda.

Author
Posts
#1489087

I want to allow multiple authors per post

I'm not sure which method to use. One idea I have is to add a custom field to the posts called "extra_authors" and allow multiple instances of that field (repeatable fieldgroup). Then I can add a custom codition like "if the loged in user exists as an author and the user email exists in the extra_authors field for the current post", then you are allowed to edit the current post.

Is it possible to filter on a repeatable fieldgroup like this? If yes, I need a hint on how to do it.

Thanks in advice

Kind regards
Rune

#1489189

It is not possible to have more than one author per post in the native WordPress features, and Toolset doesn't add any such feature either.

What you can do consists of a few possible "workarounds":

1. You can create a simple Select Field or similar and populate this with the actual users you have (value should be the ID and label the user name, probably). You can then use this field on the posts to signalize you have more than an author a whom. You can't use this later in author filters or so, of course, but you can use it in Custom Field filters and searches.

2. You can follow the steps here which make a post for each user, and so you can make connections (Many to Many, if you require) between the User (post) and actual Posts:
https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/

3. Using a Plugin hidden link. Note that it would require tests to check how and if this works with Toolset searches, for example.

Related to controlling (based on that author data) who can edit the post, you could then just wrap your edit link or ShortCode of a Toolset Form to edit the post in a HTML condition that checks on the user ID and compares it to the ID coming from the field where you store the additional author.
Of course, if you use repeatable fields this will not work, because you might have many values. Even a Checkboxes field would not be ideal for this, as you always would need to check on EACH option, and that can be intensive for the server
See also https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/#checking-types-checkboxes-field-for-non-empty-value for clarification.

Using repeatable field groups you could check conditionally on each repeatable fields group select field for example, but this could only be done in a View that displays this Repeatable Field Group (belonging to the post where the View is inserted).
Then, that View loop will display each Repeatable Field Group for the post, and since each of them could contain a User Select Field where the and additional author is saved, you could also check with HTML conditions on each of those.
But you'd get the link, or form, output more than once, since you'd be in a Loop, where many Repeatable Field Groups (one each author) would be rendered.

It is, therefore, easier to use a small Custom Code for this.
For example, you could use a Checkboxes field, and check all the Users that are authors of a post.
Then, using Custom Code and the Toolset API you would get that fields stored value and create an array with it, and check with the PHP native in_array() whether or not the current logged in user is in that array.
Then accordingly return a true or false value so you can later use this in an HTML conditional, for example.

This requires custom code but I can help with some guidance on it if this approach would suit you.

You'd start with a Checkboxes field that has as many options as users and the value of each option is a user ID.
That is best set up manually if you do not foresee a dynamic growth of users. If you instead need that field to as well dynamically get all users automatically you would need more custom code, and this field would then not work anymore in the Views search, so I would not recommend that approach but populate the user field manually instead-

Is this within your projects definitions? Then you could proceed by creating such checkboxes field and consult the ShortCode DOC and PHP DOC linked below to craft a Custom ShortCode to get, and evaluate that field.
hidden link
https://codex.wordpress.org/Shortcode_API
https://toolset.com/documentation/customizing-sites-using-php/functions/ / https://developer.wordpress.org/reference/functions/get_post_meta/

Please let me know if you need help - I cannot craft a ready to go code for you but I can guide you along
If you would require full custom code assistance we'd suggest contacting a contractor here https://toolset.com/contractors/

Thanks!

#1489247

Hi Beda

Thanks for your comprehensive response. I really appreciate it.

From what I see now, make a post for each user, and make connections (Many to Many) between the User (post) and actual Posts is perhaps the best approach.

I'll give you more details about the project and what I've done so far, so you can see if you agree:

I'm developing an Obituary site. You can see the live URL here:
hidden link

To add an Obituary you need to be registered on the site. I use a Toolset user form for this (job completed)

After registering, you can create one or more Obituary pages and pay for them using Woocommerce. I use a Toolset post form with Toolset form commerce (job competed)

Atfer creating your Obituary page(s) you can edit them and add additional content. I use a Toolset post form (Edit existing content) for this (job completed)

What I want to do now is allow the owner of the Obituary page (current author) the opportunity to add multiple author(s) if desired, so more than one person can edit the Obituary page. It's preferable to use the edit existing content form to add additional authors if possible

Thanks again for looking into this

Kind regards
Rune

#1489249

Is it possible for your Obituary owners to somehow know the ID of those users they want to add?
There is a simple workaround we could use with a small snippet, without the need of any relationship or so.

We'd simply create a single line field, call it "users-with-access" and add it to the obituary posts.
The Obituary owner has to add User ids to that field. Comma-separated, user IDs of the users who shall have access to edit this Obituary.
For example, user ID 1, 3 and 30 can edit.
So the obituary owner would add to his obituary post the IDs 1,3,30

With a simple snippet, we can then create a condition that allows hiding or showing the edit form or link only to those users.

If it's not easy for the Obituary owners to know the User ID, then yes such relation as you suggest with the Posts mimicking the user is probably the only good solution, as then the Obituary owner can pick them from a select list when connecting the posts.
To check later who can access the Form, in this case, we again have the issue that we can't easily check upon those related posts.

So, this would not be the best approach, but we could still use a Custom Code here as well to check up on the related post and compare that to the user ID who currently sees the post to check if yes or now we can let them edit the post.

Either or, we will require some custom code, I think
The first approach has a much easier custom code but the issue that you need a list of users with IDs somewhere for the obituaries owners to access and know those user ids.

#1489277

Hi Beda

Thanks for your quick reply

It should be possible for the Obituary owners to know the ID of those users they want to add. I believe it's possible to send a confirmation email to the recent registered user containing the new user's User ID. Then the new user can provide the page owner with their User ID

But instead of using the User ID, perhaps we can use the Email address? It sould be as unique as the User ID, and known to the Obituary owners as well.

What do you think?

Kind regards
Rune

#1489311

Since we will rely on a code snippet, there is little what's not possible.

It is possible to send the User ID in a Form notification using %%USER_USERID%%, to both users submitting the form or to an email (such as the Admin) or else, as visible when you create a User Form. We also have it available in the Forms API.

Whichever way you choose to let the Admin know which user requested, or should have access, the Admin will have to save this data.
The problem is we cannot use select fields as those cannot be repeating, and you need to add more than one user.

Using Repeatable Field Groups is a bit overkill for this, given we would need to query a lot of related posts to find the allowed authors.

So I suggest, a simple single-line field that holds the user IDs or emails if you prefer.
How you inform the Admin about which user needs to be added to what post, is another topic that (if you need help with), I suggest discussion in another thread.

Assuming the Admin knows what to add to which post, let's say the Admin will add Emails, he will have to add the emails of each user that has co-author access to a post to this single line field, like so:

email1@email.com,email2@email.com,email3@email.com

Then, you can craft a Custom Code that get_post_meta() this value (gets the value of the single-line field with PHP and WordPress API Code)
That value should then be exploded and put in an array (one key each email).
Then we can check if the current users' email is within that array it in_array() PHP function.

We can then return either true or false in our code, hook this to the WordPress shortcode API and use it in a HTML condition, where we will display a link to the edit form of the post, only if our code returns true (which means, our logged in user's email is within the emails stored for the post as co-author)

That code looks like follows, please consider the comments and adapt to your site:

function user_can_author( $atts ){

 	//We get the current logged in user email, see https://developer.wordpress.org/reference/functions/wp_get_current_user/
    $current_user_email = wp_get_current_user()->user_email;

    //we get the value of the single line field saved against the post, with all those emails of users that an co-author
    //https://developer.wordpress.org/reference/functions/get_post_meta/
    //We get the ID of the post with get_the_ID(), see https://developer.wordpress.org/reference/functions/get_the_id/
    $field_value = get_post_meta(get_the_ID(),'wpcf-user-can-do', true);

    //We explode tha commaseparated string to an array where the email will be value of each key
    //<em><u>hidden link</u></em>
    $field_values_array = explode( ',', $field_value );

    //Use below to debug the code
    //error_log(print_r($field_value, true));
    // error_log(print_r($field_value, true));
    // error_log(print_r($field_values_array, true));
 
 	//Here we check if the current user email is within the emails stored for $field_value
 	//<em><u>hidden link</u></em>
    if (in_array($current_user_email, $field_values_array)) {
        return true;
    }
    else {
        return false;
    }
 
}
add_shortcode( 'user-can-author', 'user_can_author' );

It relies on a single line field with slug user-can-do, where the admin adds the emails of the users in a comma-separated fashion as above explained.

Then we use it in an HTML condition like so:

[wpv-conditional if="( '[user-can-author]' eq '1' )"]
   The current user has access to this post, display a form or link to a form
[/wpv-conditional]

This should not require a lot of adaptation on your site, but the code should be reviewed and comprehended, please let me know if you have any doubts left on its function and maintenance.

#1489409
condition.jpg

Hi Beda

Most of the code looks clear and obvious to me.

However, I have a question regarding the html condition:

I have a condition in place already. Author ID from Current post is equal to Current user.

( ( '[wpv-post-author format="meta" meta="ID" ]' = '[wpv-current-user info="id"]' ) )

I need to extend this so I can allow users with Co-author access as well. I'm trying

( ( '[wpv-post-author format="meta" meta="ID" ]' = '[wpv-current-user info="id"]' ) OR ( '[user-can-author]' eq '1' ) )

but I get the error message "Please, compleate all the condition or remove them". The syntax looks ok to me. Can you see why I can't apply this condition?

See attached screenshot.

Thanks in advice.

Regards
Rune

#1491641
Bildschirmfoto 2020-02-04 um 15.08.36.png

I actually would insert the condition in a text editor and then edit it there, instead of using the GUI, in this case.

This is how I usually proceed with more complex conditions:

1. I open a text editor in the post edit area (be it a classic or fields and text block, either where you can edit clean HTML)
2. In there begin inserting an HTML condition with the "conditional output" or "If" button. Insert a simple condition, like "post title equal "dummy content"
3. Then highlight the first part of the condition (the base shortcode) and insert the more complex shortcodes eventually with attributes, using the Fields and Views GUI.
4. Repeat the same for the compared value
5. If you have more than one condition, at step #2 make sure to add all dummy conditions already and make sure to set OR/AND between each, so the basic syntax is set.

Then you only need to edit the content within the conditional syntax, replacing ShortCodes or values.

In your case, I start with the GUI inserting a very simple Dummy Condition, comparing A to B twice, in an OR condition.
That gives me

[wpv-conditional if="( '[wpv-post-author]' eq '[dummy value]' ) OR ( '[user-can-author]' eq '[another value]' )"][/wpv-conditional]

Now you can alter the actual condition data (the ShortCodes) and compare to what you really need, by highlighting the shortcodes, and then replacing them with the ones generated in the Views and Fields GUI. So we replace [wpv-post-author] with [wpv-post-author format='meta' meta='ID' ] and [dummy value] with [wpv-current-user info='id'], etc.

[wpv-conditional if="( '[wpv-post-author format='meta' meta='ID']' eq '[wpv-user field="ID"]' ) OR ( '[user-can-author]' eq '1' )"][/wpv-conditional]

Can you try this on the site to see if it helps?
Note that when you use such conditions you also have to always alternate "" and '' apostrophes, as seen in my example above (inner ShortCodes use '', the outer use "")

#1492751

To show an hide an edit link if the current author or co-author is logged in or not, I have tried this code in an editor:

[wpv-conditional if="( '[wpv-post-author format='meta' meta='ID']' eq '[wpv-user field="ID"]' ) OR ( '[user-can-author]' eq '1' )"]<p>[toolset-edit-post-link content_template_slug='rediger-minneside' target='self']Rediger minneside[/toolset-edit-post-link]</p>[/wpv-conditional]

I'm not able to see the edit link if logged in.

If you can't see anything wrong, is it possible to send you my WordPress login so you can see it in my WordPress backend?

Thanks in advice

Regards
Rune

#1493381

Try like so:

[wpv-conditional if="( '[wpv-post-author format='meta' meta='ID']' eq '[wpv-user field='ID']' ) OR ( '[user-can-author]' eq '1' )"]

ANYTHING HERE

[/wpv-conditional]

(Note the apostrophes)

Now log in as the author, or as the user stored in user-can-author field.
Do you see "ANYTHING HERE"?

If not, please add login details, and the link to where I can test, in the next private reply, so I can take a closer look

#1495793

You didn't register the custom shortcode in Toolset > Settings.
hidden link > Third-party shortcode arguments

Please add the custom shortcode there, so it can be used in an HTML conditional.
(I think I forgot to mention this earlier.)

I've done that for you and I think it works now.

#1496949

My issue is resolved now. Thank you!