Tell us what you are trying to do?
I have 2 post types dogs and owners. I have connected multiple dogs to 1 owners succesfully but when some dogs have multiple owners, I need 2 ids in the ''related_to' ' but thats not working. Any suggestions how to fix this?
Is there any documentation that you are following?
Is there a similar example that we can see?
What is the link to your site?
Testing this locally but the website is online at oesdatabase.eu
I can't find any documentation about many-to-many with php solutions. All is with View.
Hi,
Thank you for contacting us and I'd be happy to assist.
Toolset offers the 'toolset_get_related_posts' function, which can be used to get all the related parent/child posts of the target post:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
You can use this function instead of using the 'WP_Query' directly. Here is another support thread, with an example of a code for a similar requirement:
https://toolset.com/forums/topic/getting-a-list-of-related-posts/
I hope this helps and please let me know if you need further assistance.
regards,
Waqar
But I need the query to loop the content I want to show. I don't really understand how to work with this....
Thanks for writing back.
I can help you with the pointers around this custom code, but, I'll need to see exactly how and where you're planning to use it.
Can you please share temporary admin login details, along with the link to the page where you'd like to use this code? I'll be in a better position to suggest the next steps, accordingly.
Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.
Sorry for the late reply. Okay trying to explain this as good as it can be
I have a relationship many-to-many from posttype dogs with owners. A dog can have multiple owners...and owners can have multiple dogs.
I have a posttype breeders which shows the dogs from the same taxonomy 'kennelname'. They are listed under bred dogs.
I have also a list of dogs under the title owned. This one does the following magic. I have tagged the owners with the same kennelname tax. And than it shows the dogs from that owner. This works fine with the post relationships. Only it only shows the dog of 1 owner. so I thought I put this owners in an array and use this in the relationships function. But this dont work
hidden link
Thank you for sharing these details.
Based on what you've shared, I was able to get the related 'Dogs' on the single 'Breeders' post page, using the following code:
// getting current breeder's kennel taxonomy term slug
$breeder_name = do_shortcode('[wpv-post-taxonomy type="kennel-name" format="slug"]');
// setting an empty dogs array for later
$dogs_array = array();
// getting owners with the same kennel taxonomy
$owner_args = array(
'post_type' => 'owner',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'kennel-name',
'field' => 'slug',
'terms' => $breeder_name
)
)
);
$owners_array = get_posts( $owner_args );
// looping through each found owner
foreach ($owners_array as $owner_post) {
// getting dogs related to the current owner in the loop
$query_by_element = $owner_post->ID; // ID of post to get relationship from
$relationship = 'dog-owner'; // relationship slug
$query_by_role_name = 'child'; // $query_by_element is a child in this relationship
$limit = 99999; // number of results to return
$offset = 0; // no offset needed
$args = array(); //nothing needed
$return = 'post_id'; // We want Post IDs
$role_name_to_return = 'parent'; // We want related parents.
$get_related_dogs = toolset_get_related_posts(
$query_by_element,
$relationship,
$query_by_role_name,
$limit,
$offset,
$args,
$return,
$role_name_to_return
);
// if some related dogs are found merge them into a single array
if(!empty($get_related_dogs)) {
$dogs_array = array_unique(array_merge($dogs_array,$get_related_dogs));
}
}
print_r($dogs_array);
Note: The relationship 'dog-owner' I used on my test website was set in a way that the 'Dogs' post is the 'parent' (i.e. the one on the left side) and the 'Owners' post is the child (i.e. the one on the right side).
( screenshot: hidden link )
Please make sure to adjust the code to match the slugs and relationships elements, as per your website.
Thanks I will test this next weekend. At my situation the owner is parent and the dog is child. What do I need to change in your code now?
Glad I could help and take your time.
> At my situation the owner is parent and the dog is child. What do I need to change in your code now?
- In this case, you'll reverse the roles of the 'parent' and 'child' in that code.
Line# 28 will change to:
$query_by_role_name = 'parent'; // $query_by_element is a parent in this relationship
And line# 33, will become:
$role_name_to_return = 'child'; // We want related children.
Hi Waqar, finally had some time to check this. Unfortunately it isn't working yet. I work on a local website so hard to share with you.
First the code
$breeder_name = do_shortcode('[wpv-post-taxonomy type="kennel-name" format="slug"]');
didnt gave the breeder name? I changed this back but it didnt change anything. The
is empty. I have changed the parent for the child like you wrote me.
this is my code. There was also a type. The taxonomy is 'kennel', not kennel-name.
<?php
// getting current breeder's kennel taxonomy term slug
//$breeder_name = do_shortcode('[wpv-post-taxonomy type="kennel" format="slug"]');
$breeder_name = get_current_term_slug( 'kennel' );
// setting an empty dogs array for later
$dogs_array = array();
// getting owners with the same kennel taxonomy
$owner_args = array(
'post_type' => 'owner',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'kennel',
'field' => 'slug',
'terms' => $breeder_name
)
)
);
//print_r($owner_args);
$owners_array = get_posts( $owner_args );
// looping through each found owner
foreach ($owners_array as $owner_post) {
// getting dogs related to the current owner in the loop
$query_by_element = $owner_post->ID; // ID of post to get relationship from
$relationship = 'owner-dog'; // relationship slug
$query_by_role_name = 'parent'; // $query_by_element is a child in this relationship
$limit = 99999; // number of results to return
$offset = 0; // no offset needed
$args = array(); //nothing needed
$return = 'post_id'; // We want Post IDs
$role_name_to_return = 'child'; // We want related children.
$get_related_dogs = toolset_get_related_posts(
$query_by_element,
$relationship,
$query_by_role_name,
$limit,
$offset,
$args,
$return,
$role_name_to_return
);
// if some related dogs are found merge them into a single array
if(!empty($get_related_dogs)) {
$dogs_array = array_unique(array_merge($dogs_array,$get_related_dogs));
}
}
print_r($owners_array);
print_r($dogs_array);
?>
The code that I shared was an example based on the setup that I used on my website. You'll have to adjust the post type, taxonomy, and relationship slugs, as used on your website.
Without access to the website, it is very difficult to suggest why the code is not working for you. Would it be possible for you to put the website on an online server or share its clone/snapshot so that I can see it on my server?
( ref: https://toolset.com/faq/provide-supporters-copy-site/ )
I'm setting your next reply as private so that you can share the online website's access details or the website's duplicator package.