Skip Navigation

[Waiting for user feedback] How to set query in the relationship form show post based on taxonomy and author

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 8 replies, has 1 voice.

Last updated by Minesh 1 day, 16 hours ago.

Assisted by: Minesh.

Author
Posts
#2810033

jum
private group.png

Hi,

I have a group post type that have private and public taxonomy terms. Post type have M2M relationship between other post type.

In the relationship form : the group select dropdown I want to show the post based on below condition

show private term post and current user is author of the post

What I tried.

I used View to query based on the condition and gave the view shortcode relationship form. In the frontend I can see the post based on the condition but I am unable to submit the form.(screenshot attached)

[cred-relationship-form-container]
<div class="form-group" style="display:none;">
<label for="%%FORM_ID%%_parent">[cred_i18n name='parent-label']Grants[/cred_i18n]</label>
[cred-relationship-role role='parent']
</div>
<div class="form-group">
<label for="%%FORM_ID%%_child">[cred_i18n name='child-label']Groups[/cred_i18n]</label>
<select id="custom-child-select">
<option value="">Select a group</option>
[wpv-view name="xxxxxx-private-group-name-xxxxx"]
</select>
</div>

<div style="display:none;">
[cred_field field='@grant-group.child' output='raw']
</div>
[cred-form-feedback field='feedback' name='feedback']
[cred-form-submit field='submit' name='submit']
[cred-form-cancel field='cancel' name='cancel']
[/cred-relationship-form-container]

Please guide me how to set the condition in the relationship forms.

#2810054

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - to only display the options by current author, when you crerate post relationship form with parent/child fields you will see the "Filtering" section from where you can set the option. Please check the following screenshot:
- hidden link

Regarding filtering the options further, you can use pre_get_posts hook as described with the following related ticket:
=> https://toolset.com/forums/topic/filter-post-in-form-relationship/

For example:

add_action( 'wp_ajax_toolset_select2_suggest_posts_by_post_type', 'ts_mod_suggested_posts', 1 );
// if guest users can use your relationship form then uncomment the following line
// add_action( 'wp_ajax_nopriv_toolset_select2_suggest_posts_by_post_type', 'ts_mod_suggested_posts', 1 );
function ts_mod_suggested_posts(){
 
    add_action( 'pre_get_posts', 'ts_pre_get_posts' );
    function ts_pre_get_posts( $query ){
     
        if ( $query->query['post_type'] == 'left' )
        {
            // you can set additional query parameters for post type 'left' here
 
        } elseif ( $query->query['post_type'] == 'right' )
        {
            // you can set additional query parameters for post type 'right' here
            $query->set( 'meta_key', 'wpcf-priority' );
            $query->set( 'meta_value', '1' );
            $query->set( 'compare', '=' );
        }
    }
}
#2810382

jum

Hi Minesh,

Thanks the filtering option in the screenshot works fine.

But I do have another form for admin but in this I want to show all the public post and in private I want to show only the current user post. I alter the code you provided but its not working .

add_action( 'wp_ajax_toolset_select2_suggest_posts_by_post_type', 'ts_mod_suggested_posts', 1 );
// Uncomment below if guest users can use the relationship form
// add_action( 'wp_ajax_nopriv_toolset_select2_suggest_posts_by_post_type', 'ts_mod_suggested_posts', 1 );

function ts_mod_suggested_posts() {
add_action( 'pre_get_posts', 'ts_pre_get_posts' );
}

function ts_pre_get_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {

$post_type = $query->get( 'post_type' );

if ( $post_type === 'group' ) {
$current_user_id = get_current_user_id();

// Allow public posts
$tax_query = [
[
'taxonomy' => 'group-status',
'field' => 'slug',
'terms' => ['public'],
]
];

if ( $current_user_id ) {
// Also allow private posts by current user
$tax_query[] = [
'taxonomy' => 'group-status',
'field' => 'slug',
'terms' => ['private'],
];

// Only include private posts by the current user
$meta_query = [
'relation' => 'OR',
[
'key' => 'group-status',
'compare' => 'NOT EXISTS',
],
[
'key' => 'group-status',
'value' => 'private',
'compare' => '=',
]
];

$query->set( 'author', $current_user_id );
}

$query->set( 'tax_query', $tax_query );
$query->set( 'post_status', 'publish' );
}
}
}

#2810384

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share problem URL where you added the form as well as admin and frontend user access details and tell me loggedin with what user what is your expected results.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2810971

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - there is no option to filter the dropdown on admin section. Do you want to filter the dropdown on admin section?

I'm not sure where exactly you stuck.

Can you please share bit more informatin where exactly you stuck and with frontend dropdown or backend dropdown?

#2811232

jum

Hi Minesh,

I am struck in showing the filter in frontend of the form
hidden link

in this page there is a Share to group section there is a relationship form, parent field is hidden and it take the current post and in the child dropdown I want to show that have all public terms post and private post created by current use.

Please let me know if you need more information

#2811277

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share details with what user I should check against and login as frontend user?

Maybe you can setup a test user with few public and private posts and send me access details for that frontend user and tell me what public post and private post it should display and then I will try to troubleshoot further.

I have set the next reply to private which means only you and I have access to it.

#2811348

jum

Hi Minesh,

You can use the same credentials to check the logic as it is for admin

I have set some test private and public posts. I have created you as member ,one private post as you as author.

So in the frontend form it you need to show three post
two public post and one private as you as author.

Department-public
Regional-public
Toolset private group-private // this is the post where your ID as author.

Please guide me in setting this condition show all public post and private post where current user is the author.

#2811367

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - I see you set "Minesh support" as post author for the following post:
- Toolset private group-private // this is the post where your ID as author.

But what I'm saying is that - I should be login as "Minesh support" user and then I should access the following page so that I can query the post loggedin as same author.
- hidden link

Can you please share "Minesh support" user access details? or, its ok for you if I change the password for this user and then try to login?

I have set the next reply to private which means only you and I have access to it.