Skip Navigation

[Résolu] Related Posts Function

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

Problem: I am trying to use a meta_query with the post relationships API toolset_get_related_posts, but it doesn't seem to be filtering as expected.

Solution: Some complex meta_queries, like using "IN" or "AND" with more than one term, fail with the post relationships API. Instead, you should use a standard WP_Query and add the toolset_relationships clause:

$args = array(
  'numberposts'           =>   -1,
  'post_type'             =>   'child-post-type-slug',
  'orderby'               =>   'post_date',
  'order'                 =>   'DESC',
  'meta_query'            => array(
    'relation' => 'AND',
    array(
        'key'     => 'wpcf-colour',
        'value'   => 'Red',
        'compare' => '='
    ),
    array(
        'key'     => 'wpcf-description',
        'value'   => 'Ball',
        'compare' => '='
    )
  ),
  'toolset_relationships' => array(
    'role'          => 'child',
    'related_to'    => $post_id,
    'relationship'  => 'relationship-slug'
  ),
);
$child_query = new WP_Query( $args );
This support ticket is created Il y a 4 années et 9 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 2 réponses, a 2 voix.

Dernière mise à jour par julieP Il y a 4 années et 9 mois.

Assisté par: Christian Cox.

Auteur
Publications
#1537343

I'm using the toolset_get_related_posts function as clarified by Nigel in this ticket:- https://toolset.com/forums/topic/for-beda-please-re-opening-previous-ticket/

In a cred save_data hook (when parent post submitted), I need to update postmeta in one of the child posts (the only one with postmeta values matching the args in the related posts function). However when there are multiple child posts, the wrong post is updating. So I put the function into a shortcode and could see that posts are being included that don't match the criteria. Here's the function:-

$children = toolset_get_related_posts(
            $post_id,
            'relationship-slug',
            array(
                'query_by_role'     =>  'parent',
                'role_to_return'    =>  'child',
                'return'            =>  'post_id',
                'args'              =>  array( 
                    'post_status' => 'publish',
                    'meta_query' => array(
                        'relation' => 'AND',
                        array(
                            'key'     => 'wpcf-colour',
                            'value'   => 'Red',
                            'compare' => '='
                        ),
                        array(
                            'key'     => 'wpcf-description',
                            'value'   => 'Ball',
                            'compare' => '='
                        )
                    )
                )
            )
        );

If I have two children, 'description' is Ball for both but one is Red and the other Black, the function returns both posts. I can't work out why I'm not getting the expected results. Can you help please?

#1537829

Does it work with a standard WP_Query, using the toolset_relationships parameters? I've seen issues with complex meta_query's in post relationships API.

$args = array(
  'numberposts'           =>   -1,
  'post_type'             =>   'child-post-type-slug',
  'orderby'               =>   'post_date',
  'order'                 =>   'DESC',
  'meta_query'            => array(
    'relation' => 'AND',
    array(
        'key'     => 'wpcf-colour',
        'value'   => 'Red',
        'compare' => '='
    ),
    array(
        'key'     => 'wpcf-description',
        'value'   => 'Ball',
        'compare' => '='
    )
  ),
  'toolset_relationships' => array(
    'role'          => 'child',
    'related_to'    => $post_id,
    'relationship'  => 'relationship-slug'
  ),
);
$child_query = new WP_Query( $args );

You'll have to replace child-post-type-slug and relationship-slug.

#1540483

Hi Christian

I did wonder if the meta_query were too complex; I've tried numerous alternative ways of "extracting" the related post/s including the one you provided and all are returning expected results.

It's shame the toolset function falls short but at least now I know when to use it (and not!).

Thanks for your help.