Skip Navigation

[Resolved] $args argument in toolset_get_related_posts() does not work with multiple custom

This support ticket is created 5 years, 10 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by Jackie 5 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#1219927

Hi there,

It seems to be the case that passing meta_query arguments to toolset_get_related_posts() does not retrieve the correct posts when multiple custom fields are passed to the WP query. (Note: I've been referencing the following Toolset documentation: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts.)

The following code deals with one custom field and works fine:

    $query_args = array ( 'meta_key' => 'wpcf-status', 'meta_value' => 'open', ) ;
    $catalog_sections_open = toolset_get_related_posts (
	    $catalog_id, // ID of starting post.
	    'catalog-to-section-relationship',
	    array (
		    'query_by_role'     => 'parent', // Start from the parent.
		    'limit'             => 999,
		    'return'            => 'post_id', // Return an array of post IDs.
		    'role_to_return'    => 'child', // Return the child.
            'args'              => $query_args,
            )
    ) ;

It finds sections whose status is "open." What I really want is to find sections whose status is either "open" or "almost full." The following code does _not_ work:

    $query_args = array (
            'meta_query' => array (
                    'relation' => 'OR',
                    array (
                            'key' => 'wpcf-status', 'value' => 'open', 'compare' => '=',
                        ),
                    array (
                            'key' => 'wpcf-status', 'value' => 'almost full', 'compare' => '=',
                        ),
                )
    ) ;
    $catalog_sections_open = toolset_get_related_posts (
	    $catalog_id, // ID of starting post.
	    'catalog-to-section-relationship',
	    array (
		    'query_by_role'     => 'parent', // Start from the parent.
		    'limit'             => 999,
		    'return'            => 'post_id', // Return an array of post IDs.
		    'role_to_return'    => 'child', // Return the child.
            'args'              => $query_args,
            )
    ) ;

Using the Query Monitor plugin, I examined the SQL query that gets constructed, and the query for the first snippet adds the correct portion of the WHERE clause, whereas the second snippet omits that part of the WHERE clause altogether.

The Toolset documentation, referenced above, says that the 'args' value is supposed to be just like the WP query object arguments, but I'm not sure it is!

Can someone offer some insight? I'm happy to share more of the code and answer questions related to it, too.

Thanks!

Saul

#1220175

Dear Saul,

There isn't parameter "relation" in function API function toolset_get_related_posts(), that function supports only simple meta query, see the document you mentioned above.

In your case, I suggest you try WordPress class WP_Query, for example:

$query = new WP_Query( 
    array(
        'post_type' => 'section', //Child post type slug
        'numberposts' => -1,
        'toolset_relationships' => array(
            'role' => 'child',
            'related_to' => $catalog_id, // ID of starting post
            'relationship' =>'catalog-to-section-relationship',
        ),
	'meta_query' => array(
		array(
			'key'     => 'wpcf-status',
			'value'   => array('open', 'almost full'),
			'compare' => 'IN',
		),
	),
    )
);
$related_section_posts = $query->posts;

More help:
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

#1263381

Luo,

Though it's a couple of months after you posted a reply, this response solved my issue.

I will say that the documentation on https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts says that $args "Works exactly like in WP_Query." And that's very misleading, because it works with simple queries but not complex ones. I would make that clearer in the documentation.

Thanks again for your help!

Saul