Skip Navigation

[Resolved] Many to many using the WP_query

This support ticket is created 6 years, 6 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by michaelS-51 6 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#949108

Hello Toolset,
I want to use the WP_query instead of the types_child_posts function to have more control but its not working.

I have created a many to many relation with cpt's tribe_events and auteur.

I use the following code

<?php
$child_args = array(
'post_type' => 'tribe_events',
'numberposts' => -1,
'order' => 'ASC',
'toolset_relationships' => array(
'role' => 'child',
'related_to' => get_the_ID(),
'relationship' => array( 'auteur', 'tribe_events' )
)
);

$query = new WP_Query( $child_args );
$child_posts = $query->posts;
foreach ($child_posts as $child_post );
{ ?>

<div class="">
<h3><?php echo $child_post->post_title; ?><h3>
<?php echo get_the_post_thumbnail( $child_post, 'thumbnail' ); ?>
</div>
<?php } ?>

Could you help?
Regards

#949251

Hi, was this M2M relationship created after migrating to the new relationships system, or on a site that never used relationships before Types 3.0? If either of those is the case, then you cannot use the array syntax to indicate the post relationship like this:

'relationship' => array( 'auteur', 'tribe_events' )

The array syntax is for legacy relationships or relationships migrated from the legacy system. Instead, you must use the relationship slug, something like this:

'relationship' => 'auteur-tribe_events'

You can find the relationship slug by going to Toolset > Relationships and editing this relationship. Check the documentation here for more information: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#using-the-new-post-relationships

#950117
toolset.jpg

Hi Chris,
Thank you for the reply but no success. The relation is made after the migration.
the relation slug is auteur-evenement. See screenshot
This is the full code i now use on the auteur template:
<?php
$child_args = array(
'post_type' => 'tribe_events',
'numberposts' => -1,
'order' => 'ASC',
'toolset_relationships' => array(
'role' => 'child',
'related_to' => get_the_ID(),
'relationship' => 'auteur-evenement'
)
);

$query = new WP_Query( $child_args );
$child_posts = $query->posts;
foreach ($child_posts as $child_post );
{ ?>

<div class="">
<h3><?php echo $child_post->post_title; ?><h3>
<?php echo get_the_post_thumbnail( $child_post, 'thumbnail' ); ?>
</div>
<?php } ?>
But it only shows 1 tribe event where i have connected 2.
This code with changed post_type' => 'auteur' in the tribe event template doenst work at all.
Could you please help?

#950171

New code but still shows 1 auteur. changing the order to desc shows a different auteur.

<?php
$child_args = array(
'post_type' => 'tribe_events',
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => 'menu_order',
'toolset_relationships' => array(
'role' => 'child',
'related_to' => get_the_ID(),
'relationship' => 'auteur-evenement'
)
);

$query = new WP_Query( $child_args );
$child_posts = $query->posts;
foreach ($child_posts as $child_post );
{ ?>

<div class="">
">

post_title; ?>



</div>
<?php } ?>

#950445

If you're putting this code directly into a PHP template, you need to see the comment about timing and priority here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#remarks

To determine if the timing is causing the problem, please wrap the same query in a shortcode and examine the results:

function temp_func($atts) {
    $child_args = array(
        'post_type' => 'tribe_events',
        'posts_per_page' => '-1',
        'order' => 'ASC',
        'orderby' => 'menu_order',
        'toolset_relationships' => array(
            'role' => 'child',
            'related_to' => get_the_ID(),
            'relationship' => 'auteur-evenement'
        )
    );

    $query = new WP_Query( $child_args );
    $child_posts = $query->posts;

    return print_r($child_posts, true));
}

add_shortcode( 'temp_test', 'temp_func');

Insert the shortcode in the Auteur single post content and check the results:

[temp_test]

You could also test with a hard-coded ID for related_to instead of get_the_ID(), to be sure context isn't causing a problem with the current ID.

#951189

Hi Chris,
i did what you suggested and the page shows the following result (i replaced the url guid with **)

Array ( [0] => WP_Post Object ( [ID] => 2025 [post_author] => 9 [post_date] => 2018-06-26 09:57:25 [post_date_gmt] => 2018-06-26 09:57:25 [post_content] => test [post_title] => testdavid [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => testdavid [to_ping] => [pinged] => [post_modified] => 2018-07-18 11:19:03 [post_modified_gmt] => 2018-07-18 09:19:03 [post_content_filtered] => [post_parent] => 0 [guid] => hidden link [menu_order] => 0 [post_type] => tribe_events [post_mime_type] => [comment_count] => 0 [filter] => raw [EventStartDate] => 2018-09-01 08:00:00 [EventEndDate] => 2018-09-01 17:00:00 ) [1] => WP_Post Object ( [ID] => 2044 [post_author] => 9 [post_date] => 2018-07-18 11:34:22 [post_date_gmt] => 2018-07-18 09:34:22 [post_content] => Een superfeest [post_title] => Superfeest! met een lekkere lang titel om te testen! [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => superfeest [to_ping] => [pinged] => [post_modified] => 2018-07-18 14:16:54 [post_modified_gmt] => 2018-07-18 12:16:54 [post_content_filtered] => [post_parent] => 0 [guid] => hidden link [menu_order] => 0 [post_type] => tribe_events [post_mime_type] => [comment_count] => 0 [filter] => raw [EventStartDate] => 2018-10-19 10:00:00 [EventEndDate] => 2018-10-19 12:30:00 ) )

#951303

Hi Chris,
For somehow the WP_query wasnt able to find a specific child post. I saved the child post again and then was able to reconnected the post to the parent.
It wasnt a code thing.

Thank you for your help.
Regards