Skip Navigation

[Résolu] toolset_relationships doesn’t work for me

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I am trying to use the toolset_relationships query argument with get_posts to get related posts, but it doesn't seem to be working as expected.

Solution: Pay close attention to the remarks regarding timing and the WordPress request lifecycle. Some features are not available until specific events have taken place. Note that you must use suppress_filters => false with get_posts. If all else fails, use the Post Relationships API directly instead of WP_Query or get_posts.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#remarks
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#wp_query-argument-for-querying-by-related-posts
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created Il y a 5 années et 11 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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)

Ce sujet contient 6 réponses, a 2 voix.

Dernière mise à jour par DM Il y a 5 années et 11 mois.

Assisté par: Christian Cox.

Auteur
Publications
#1170708

DM

I am trying to:

Perform a query within a custom function in functions.php. It appears as though the toolset_relationships part of the query is ignored.

$args = array(
        'post_type' => 'fast-fact',
        'post_status'     => 'publish',
        'numberposts' => -1,
        'toolset_relationships' => array(
            'role' => 'child',
            'related_to' => $post->ID,
            'relationship' => 'destination_fast-fact'
        )
    );

Link to a page where the issue can be seen:

No page, this is local on my laptop.

I expected to see:

I expect it to limit the posts to just those that are a child of the post.

Instead, I got:
It returns all child posts, not just the ones for that post.

#1170743

Hi, let's confirm these things first:
- Check the remarks section here for information about timing and when the toolset_relationships query is available in the request lifecycle: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#remarks
- If you have not already, echo or log $post->ID just before this $args array and confirm it is the numeric ID of a publicly published "destination" post.
- Double-check the relationship slug. You can inspect it in Toolset > Relationships.
- Double-check the parent and child aliases. When editing the Toolset Relationship, check the confirmation checkbox and you'll be shown which post is parent and which is child. Looks like Fast-fact should be the child and Destination should be the parent.
- Update to the latest version of Types, then temporarily deactivate all other plugins.
- Test again. If the filter works, reactivate your plugins one by one until the problem returns.
- If the filter still does not seem to work, I'll have to take a closer look.

#1170762

DM

Hi Christian,

I'm guessing it had something to do with the lifecycle. This query works when I use it within the context of a page to display posts, but when I try to reference it in my functions.php, it doesn't seem to register.

So to be specific, I'm trying to add extra meta info from child posts to include in the search index of SearchWP.

lien caché

And here's a better example of how I am doing it:

lien caché

Here's how I'm doing it...

function my_searchwp_extra_metadata($extra_meta, $post_being_indexed) {

	if ( 'destination' !== get_post_type( $post_being_indexed ) ) {
		return $extra_meta;
    }

    $args = array(
        'post_type' => 'fast-fact',
        'post_status'     => 'publish',
        'numberposts' => -1,
        'posts_per_page' => 150,
        'suppress_filters' => true,
        'toolset_relationships' => array(
            'role' => 'child',
            'related_to' => $post_being_indexed->ID,
            'relationship' => 'destination_fast-fact'
        )
    );

    $destination_posts = get_posts($args);

    if (! empty($destination_posts)) {
        $extra_meta['fast_facts'] = array();
        foreach ($destination_posts as $destination_post) {
            $post_meta = get_post_meta( absint( $destination_post->ID ), 'wpcf-fact-description', true );
            $extra_meta['fast_facts'][] = $post_meta;
        }
    }

    return $extra_meta;
}

add_filter('searchwp_extra_metadata', 'my_searchwp_extra_metadata', 10, 2);

So I guess the big question is...how do I retrieve child posts within this context using Toolset?

#1170764

You must set suppress_filters => false when using get_posts:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#wp_query-argument-for-querying-by-related-posts

Try that first, then if it doesn't help you can try using the actual Toolset Post Relationships API instead of a direct WP_Query or get_posts: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

#1170798

DM

I've updated the code to this and it seems to work, but I'm not sure I can make sense of it.

function my_searchwp_extra_metadata($extra_meta, $post_being_indexed) {

    // we only care about WooCommerce Products
	if ( 'destination' !== get_post_type( $post_being_indexed ) ) {
		return $extra_meta;
    }

    $fast_facts = toolset_get_related_posts (
        $post_being_indexed->ID,
        'destination_fast-fact',
        'parent',
        100000,
        0,
        array(),
        'post_object',
        'child'
    );

    if (! empty($fast_facts)) {
        $extra_meta['fast_facts'] = array();
        foreach ($fast_facts as $fast_fact) {
            $post_meta = get_post_meta( absint( $fast_fact->ID ), 'wpcf-fact-description', true );
            $extra_meta['fast_facts'][] = $post_meta;
        }
    }

    return $extra_meta;
}
add_filter('searchwp_extra_metadata', 'my_searchwp_extra_metadata', 10, 2);

Does the query make sense?

#1171952

Yes it looks pretty good to me, If you're only using the post ID there's no reason to return the full post_object in the toolset_get_related_posts call. You can replace "post_object" with "post_id", and then modify the get_post_meta call like so:

$post_meta = get_post_meta( absint( $fast_fact ), 'wpcf-fact-description', true );

But as it is now is fine and probably not noticeably slower.

#1172024

DM

Thanks for confirming. I went ahead and made that change and all works!