Skip Navigation

[Resolved] Select/filter posts with certain term in relationship dropdown in cred form

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

Problem:
Select/filter posts with certain term in relationship dropdown in cred form

Solution:
You will have to use the "pre_get_posts" hook in order to filter the parent dropdown posts based on specific taxonomy term.

You can find proposed solution in this case with the following reply:
https://toolset.com/forums/topic/select-posts-with-certain-term-in-relationship-dropdown-in-cred-form/#post-2310995

Relevant Documentation:
=> https://kb.onthegosystems.com/code-snippet/filter-the-parent-post-selector-select2-input-in-toolset-forms-by-whatever-you-want/

This support ticket is created 2 years, 8 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 9 replies, has 2 voices.

Last updated by sarahK-2 2 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2307043

I have a Teams-to-Members relationship (One-to-Many ) .

On the individual Member account page there is a form where the Member can update which Team they belong too. Currently, there is a dropdown of all the Teams, and they select which team they would like to switch their Membership to.

However, not all Teams are currently accepting new Members. Teams that have indicated they will take on new Members have marked the custom Taxonomy "Actives" as "Active". I would like to have only the Teams with the correct taxonomy ("active") displayed and able to be selected in the dropdown list.

How do I do this?

#2307161

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I would like to know first are you using post-relationship form or post edit form to update the post-relationship between member and team?

Can you please share problem URL of the member account page where I can see the form as well as admin access details and let me see what we can do here.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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.

#2307313

It is a Post Edit form for the post type "Member". The form works great to update the member's team affiliation. The post type Member is connected (via authorship) to the user account, and post type "Member" already exists at the time the user goes to make this edit. The only issue is that I would like to only show Teams with tag "active" in the dropdown list, instead of all the teams we have ever had. I'm assuming I should put something in there that filters the taxonomy.

It is not that I have tried to filter the dropdown list by taxonomy and it hasn't worked... I don't know how to do this at all so haven't even tried. If there is not a simple answer for this I will duplicate the post types and forms on a dev site and you can take a look.

#2307347

Minesh
Supporter

Languages: English (English )

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

There is no native feature to filter the parent post dropdown with the specific taxonomy term.

However, here is the code snippet that should work for you as a workaround.
- hidden link

You can adjust the values of post type and even make sure that the snippet should run at your desired post/post type only. If you do not know how to do it, you are welcome to share the test site access details and I'm happy to set it up for you.

#2307995

Thanks for your reply. I tried to use the snippet provided, but clearly I am doing something wrong. I would love your help, please facilitate me getting the admin login info to you.

#2308193

Minesh
Supporter

Languages: English (English )

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

Please share problem URL where you added the form and with what field you want to filter it with what term?

*** 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.

#2310043

Minesh
Supporter

Languages: English (English )

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

I've added the following filter code to "Custom Code" section offered by Toolset:
=> hidden link

function filter_parent_post_select2_by__taxonomy_term( $query ){
    global $post;
  
if($_REQUEST['cred_post_id'] == 324) {   // post id where you added the form
  
    if (defined('DOING_AJAX') && DOING_AJAX) { 
       //check for the action & slug to focus in on a specific dropdown if you have multiple dropdown in the form
       if($_REQUEST['action']=='select2_potential_relationship_parents'){
         	
            if($query->query['post_type'][0]=='team'){
                 $taxquery = array(   array(
                                     'taxonomy' => 'active',
                                     'field' => 'slug',
                                    'terms' => array('active'),
                                    'operator'=> 'IN' ));
                $query->set( 'tax_query', $taxquery );
            }
        }       
    }
}
}
add_action( 'pre_get_posts', 'filter_parent_post_select2_by__taxonomy_term', 101, 1 );

I can see its filtering the parent post dropdown with taxonomy term active correctly. Can you please confirm it works at your end as well.

#2310845

Thank you, yes, it works, but only on that one individual Member - not on each and every Member Post.

if($_REQUEST['cred_post_id'] == 324)

324 is the post ID of one particular Member. But all members need to be able to update their Team the same way. The form is on the Single Post Template for the Custom Post Type "Member".

The number of the Member Single Post Template is 333. I tried this to see if it would work then on all members, but it did not.
The number of the Cred Form itself is 337. I tried this as well, but it did not work either.

Not sure where to go from here.

#2310995

Minesh
Supporter

Languages: English (English )

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

Ok. I understand that you want to add the form on all single post of post type "member":

I've adjusted the code as given under:

function filter_parent_post_select2_by__taxonomy_term( $query ){
    global $post;
 $mypost =  get_post($_REQUEST['cred_post_id']);
if($mypost->post_type == 'member') {   // post id where you added the form

  
    if (defined('DOING_AJAX') && DOING_AJAX) { 
       //check for the action & slug to focus in on a specific dropdown if you have multiple dropdown in the form
       if($_REQUEST['action']=='select2_potential_relationship_parents'){
         	
            if($query->query['post_type'][0]=='team'){
                 $taxquery = array(   array(
                                     'taxonomy' => 'active',
                                     'field' => 'slug',
                                    'terms' => array('active'),
                                    'operator'=> 'IN' ));
                $query->set( 'tax_query', $taxquery );
            }
        }       
    }
}
}
   add_action( 'pre_get_posts', 'filter_parent_post_select2_by__taxonomy_term', 101, 1 );

Can you please confirm it works as expected on single posts of post type "member".

#2311033

Works perfectly now.

As usual, the support at Toolset is above and beyond. Truly the best.