Home › Toolset Professional Support › [Resolved] 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 13 replies, has 1 voice.
Last updated by jum 1 month, 1 week ago.
Assisted by: Minesh.
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.
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', '=' ); } } }
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' );
}
}
}
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.
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?
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
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.
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.
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.
Hi Minesh,
Sorry I missed to change the author to Minesh support. I have changed it now to you. So you login with the credentials I provide before and go to the page and in that it need to show only your private and all public
- hidden link
If require you can create a new group for testing.
Now I'm not sure - I do not able to access the page you shared:
- hidden link
Can you please tell me where I can see the above page?
Hi,
Can you please try this link
hidden link
please have a look and let me know if the link works
It was really a challenging task and at last I'm happy that I've a solution for you that exactly matches your requirement.
I've adjusted the code added to "Custom code" section as given under with the code snippet namely "grouplistforadmin":
=> hidden link
///// filter relationship form parent field options add_action( 'pre_get_posts', 'func_filter_relationship_form_parent_select2_options',10,1 ); function func_filter_relationship_form_parent_select2_options( $query ) { if ( defined('DOING_AJAX') && DOING_AJAX) { if($_REQUEST['form_id']==191075 and $_REQUEST['action']=='cred_association_form_ajax_role_find'){ $public_posts_ids = $private_posts_ids = array(); $current_user_id = get_current_user_id(); $post_type = (array) $query->get( 'post_type' ); if ( in_array('uni-group',$post_type) ) { remove_action( 'pre_get_posts', 'func_filter_relationship_form_parent_select2_options',10,1 ); $find_public_posts_ids = new WP_Query( array( 'post_type' => 'uni-group', 'numberposts' => -1, 'fields'=>'ids', 'tax_query' => array( array( 'taxonomy' => 'group-status', 'field' => 'slug', 'terms' => array('public'), 'operator' => 'IN', )))); $public_posts_ids = $find_public_posts_ids->posts; $current_user_id = get_current_user_id(); if ( $current_user_id ) { $find_private_posts_ids = new WP_Query( array( 'post_type' => 'uni-group', 'numberposts' => -1, 'author'=>$current_user_id, 'fields'=>'ids', 'tax_query' => array( array( 'taxonomy' => 'group-status', 'field' => 'slug', 'terms' => array('private'), 'operator' => 'IN' )))); $private_posts_ids = $find_private_posts_ids->posts; } add_action( 'pre_get_posts', 'func_filter_relationship_form_parent_select2_options',10,1 ); $public_private_posts = array_merge($public_posts_ids,$private_posts_ids); $query->set( 'post__in', $public_private_posts ); } } } }
Can you please confirm it works as expected:
=> hidden link
Hi Minesh,
It works as expected. Thanks again for the wonderful support.