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 );
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.
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.
Please update here when your test site is ready for debug, thanks
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
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?
I am checking it in your website, will update here if find anything
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
Thank you! That all works perfectly.