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