Skip Navigation

[Resolved] Filter Relationship field by something else than author

This support ticket is created 5 years, 7 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 1 reply, has 2 voices.

Last updated by Beda 5 years, 7 months ago.

Assisted by: Beda.

Author
Posts
#1242359

I have this here field:

[cred_field field='@project-persona.parent' class='form-control' output='bootstrap' select_text='--- not set ---' author='$current']

Where I am assuming $current is the current user.
can I set author to be the output of a function or shortcode?

Like so:

[cred_field field='@project-persona.parent' class='form-control' output='bootstrap' select_text='--- not set ---' author='user_that_can_edit_this_field()']

or

[cred_field field='@project-persona.parent' class='form-control' output='bootstrap' select_text='--- not set ---' author='[]user_that_can_edit_this_field']

I have set a repeatable field with User ID's for the Project post type. I have a one to many relationships between project and persona (one project can have many personas).

A project will have specific users assigned to it, denoted by a User ID repeatable numeric field. How can I filter the projects displayed in the post relaltionship field to display only projects where the current user ID matches one of the id's recorded in the User ID custom field belonging to the projects custom post type?

Thank you

Adrian

#1242493

The shortcode you mention (see below again) is created in Toolset Post Forms to add or edit new "Child" or better "The many End of a Relationship" is created, and the option "Get only options by the current author" is set:

[cred_field field='@leftside-rightside.parent' class='form-control' output='bootstrap' select_text='--- not set ---' author='$current']

author='$current' then makes sure, that when using the Form you'll only be able to choose posts to relate which you authored (as the logged in user of the Form).

Now to whether you can alter that attribute's value to something like "other_users_id", and hence return posts neither by the current author but also not an author, instead the authors of a certain custom post's query, for example, produced by a Custom Code function, there is no such inbuilt method in Toolset Forms.

The options are either by all authors or by the logged in user as author.

It would be a good request to suggest here https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/, asking to allow custom callbacks to populate that query.

For now, the only way I found to achieve this is to use a Custom Code snippet adding an (undocumented) filter to your theme's functions.php, with which you filter the $force_author return value of the core plugin code where we also ensure to load or not only current user posts.
You'd hook into cred_force_author_in_related_post filter of Toolset:

function load_specific_users_as_author( $force_author) {
    //Dull example of loading only posts of author with ID 1
    $force_author = 1;

    //Or, alter the $force_author as you like, it must return one Users ID. It does not accept arrays.
    
    //Return the $force_author 
    return $force_author;
}

//Apply the filter
add_filter( 'cred_force_author_in_related_post', 'load_specific_users_as_author', 10 );

Further customizations would be subject to Custom Code, for which you could get assistance here https://toolset.com/contractors/, and other more granulated features in the API (for example allowing to pass a whole array of User IDs) would be subject to a new Feature Request, for which I shared the link above.

I hope with this you can create what you need!

PS.
Note that the cred_field to select related posts must always have a single users ID and does not accept an array of users, see code snippet comments.