Skip Navigation

[Resolved] Query for one to many relationship help

This support ticket is created 4 years, 2 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/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by julieE-2 4 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#1849299

I have the post types documents & cases in a one to may relationship where 1 case can be assigned to 1 document & many documents to 1 case.

I have a front end view for documents archive with a filter for cases which works fine.

Now on a post form for documents I need to assign a incremental running number to the new document. So I need to count the documents assigned to a given case.

This is the query and it returns 0. ( i named the documents post type 'case-document' which is confusing)


$query = new WP_Query( 
 array(
  'post_type' => 'case-document',
  'posts_per_page' => -1,
  //new toolset_relationships query argument
  'toolset_relationships' => array(
   'role' => 'parent',
   'related_to' => $case,
   'relationship' => 'case-document'
  )
 )
);
$posts = $query->posts;  
echo count($posts);

I wonder if the one to many relationship is the one to use here, because on the case post is no reference connection to documents when I create a new document but I do have the case post reference field on documents.

What am I missing here? Basically I need the query from the documents archive view with the case filter.

Thanks

#1849443

Hi,

Thank you for contacting us and I'd be happy to assist.

Can you please adjust your query arguments like this:


$target_case = 123;
$document_post_slug = 'case-document';
$post_relationship_slug = 'case-case-document';

$query = new WP_Query( 
	array(
		'post_type' => $document_post_slug,
		'posts_per_page' => -1,
		'toolset_relationships' => array(
			'role' => 'child',
			'related_to' => $target_case,
			'relationship' => $post_relationship_slug
		)
	)
);

Note: You'll replace the values for the "$target_case", "$document_post_slug", and "$post_relationship_slug" with respect to your website.

If one case can have multiple documents in a one-to-many relationship, then it means that the case post is the parent and the document post is the child.

Please note how I've used "child" for the "role" as the document post that is being queried is a child and used the post-relationship slug for the "relationship".

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1851345

My confusion came from not understanding that in creating a post reference field, Toolset automatically creates a relationship with this field slug. I had created an extra one-to-many relationship with different slug and was using that slug. I deleted that relationship and updated the slug in the query and that worked.

My issue is resolved now. Thank you!