Skip Navigation

[Resolved] Post with term in relationship dropdown, multiple snippets, many-to-many

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

Problem:

Filter results on taxonomy field of post form and relationship form.

Solution:

It needs custom codes, see example here:

https://toolset.com/forums/topic/post-with-term-in-relationship-dropdown-multiple-snippets-many-to-many/#post-2343683

Relevant Documentation:

This support ticket is created 2 years, 9 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

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

Assisted by: Luo Yang.

Author
Posts
#2327167

I am using a snippet (shown below, and provided by you in a prior support ticket:
https://toolset.com/forums/topic/select-posts-with-certain-term-in-relationship-dropdown-in-cred-form/)

It limits the dropdown display in a relationship form club:member (one:many) to posts with a particular taxonomy term, and it is working well.

However, I am running into a couple of challenges.

First, I was able to successfully duplicate this snippet to do the same thing with another one:many relationship, on another form. If I inactivate the original snippet, the new snippet works. If both snippets are active at the same time, I get a fatal error on the site. Obviously I need to change something else so that both snippets can run without interference, but I don't know what.

Second, I would like to be able to use this for many:many relationship forms as well (where one side of the relationship is known). How can I alter the snippet to make this work?

Thanks in advance!

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]=='club'){
$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 );

#2328485

Hello,

Since it is a custom codes problem, please provide a test site with the same problem, also point out the problem post URL, where I can edit your custom PHP codes, I need a live website to test and debug it.

#2333223

Still need help but need to duplicate the problem on test site. Will let you know as soon as I have done that, and thank you.

#2334839

Please update here when your test site is ready for debug, thanks

#2338707

Thanks for the details.

Q1) First question is how to get both of these (and more, if necessary) to work at the same time.
You just need to use different PHP function name, for example, change the custom code "test-second", line 10 from:
function filter_parent_post_select2_by__taxonomy_term( $query ){

To:
function filter_parent_post_select2_by__taxonomy_term_2( $query ){

Q2) "many" does not work at all. This is for a many:many relationship (lots:severals, as seen in the form on CPT Lots)
Please elaborate the questions with more details, point out the problem page URL and form URL, where I can see the result, I need to test and debug it, thanks

#2342453

Luo,

#1: Thank you, great.

#2: The credentials in my last private message are still active. You can see the form to add CPTs "several" to a given CPT "lot" (many:many relationship) at: /dev/lot/pocket/

The current snippet is in: settings,custom code,"many".

When I use the snippet to restrict a dropdown to a particular taxonomy in a one:many relationship, it works. When I use it to restrict a dropdown to a particular taxonomy in a many:many relationship, it doesn't work. The other two snippets are examples that are currently working. The snippet "many" does not work (as seen in the form on /dev/lot/pocket/ -- it should only show "three" and "seven" as choices in the dropdown.

If you need me to give you credentials again, direct urls, etc., can you open up the private response?

#2343569

I am checking it in your website, will update here if find anything

#2343683

I have changed the "many" custom codes as below:

add_action( 'pre_get_posts', 'filter_parent_post_select2_by__taxonomy_term2', 999, 1 );

function filter_parent_post_select2_by__taxonomy_term2( $query ){
  if (defined('DOING_AJAX') && DOING_AJAX) {
    	if($_REQUEST['action']=='cred_association_form_ajax_role_find' && $_REQUEST['form_id']==503){ // specific relationship form ID 503
            // you can set additional query parameters for post type 'several' here
              $taxquery = array(
                array(
                  'taxonomy' => 'active',
                  'field' => 'slug',
                  'terms' => array('active'),
                  'operator'=> 'IN'
                )
              );
          $query->set( 'tax_query', $taxquery );
          return;
        }
  }
}

Please test again, check if it is fixed, thanks

#2344211

Thank you! That all works perfectly.