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
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
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!