Skip Navigation

[Resolved] WP_Query with multiple relationships

This support ticket is created 3 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 3 replies, has 2 voices.

Last updated by Luo Yang 3 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1654597

I'm trying to retrieve a llist consisting of 2 types of articles from an author (mixed). I have 2 relationships set up - author-webexclusive-author and author-fulltextarticle-author and am using get_the_ID() to retrieve the current Author archive page. It works fine for each query with a single relationship, (for example 'relationship' => 'author-webexclusive-author' ) but I can't seem to get the results for both at once.

I've tried using variations of the code found here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

Below is an example of the code that I've tried. What am I missing?

$the_query = new WP_Query(
array(
'post_type' => array ('webexclusive','fulltextarticle'),

'toolset_relationships' => array(

'role' => 'child',
'related_to' => get_the_ID(),
'relationship' => array('author-webexclusive-author','author-fulltextarticle-author')

),

'orderby' => 'date',
'order' => 'DESC','paged' => $paged,'posts_per_page' => 20
)
);

#1654899

Hello,

Yes, you are referring the correct document:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#advanced-example

Section "Advanced example", there is an example codes for retrieve the related posts by two relationships.

How do you setup the post types and relationships?
Where do you display above post view? How do you setup the "current Author archive page"?

Please elaborate the question with more details:
"consisting of 2 types of articles from an author"

Thanks

#1655463

There is an Author post type, and then Web Exclusive Articles and Full Text Articles. The authors are connected to these articles types; some wrote just Web Exclusive Articles, some just Full Text Articles. On the Author pages I need to list ALL the articles (Web Exclusive Articles and Full Text Articles). The post types and relationships were set up through Toolset (many to many relationships).

The Author pages are 'single' templates (single-author.php). I tried another variation of the code:
$thisauthorid= get_the_ID();
$the_query = new WP_Query(
array(
'post_type' => array ('webexclusive','fulltextarticle'),
'posts_per_page' => -1,
'toolset_relationships' => array(
array(
'role' => 'child',
'related_to' => $thisauthorid,
'relationship' => 'author-webexclusive-author'
),
array(
'role' => 'child',
'related_to' => $thisauthorid,
'relationship' => 'author-fulltextarticle-author'
)
)
)
);

This does not work either - no results. IF I were to set both relationships to be the same (like put 'relationship' => 'author-webexclusive-author' in both places) then it does get the Full Text Articles. But whenever I include 2 different relationships, it fails to return anything.

Is it perhaps looking only for post that are part of BOTH relationships? If so, that is not what I want, I need it to be EITHER relationship. An article will never be a Web Exclusive Article and a Full Text Article. In this case, what adjustment would be needed?

#1656531

Thanks for the details, the Toolset relationship filters is using "AND" logic in multiple relationship filters.
In your case:
The "Web Exclusive Articles" post is connect to "Author" post , it does not connect to any other "Full Text Articles" post , so your PHP codes won't work, and it is expected result:
it fails to return anything

It needs to setup "OR" logic between those relationship filters, but there isn't such kind of built-in feature within Toolset relationships filters, see the document I mentioned above:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#full-documentation-of-the-query-argument
section "Full documentation of the query argument"

So you will need to retrieve them separately, for example, setup two PHP codes:
- The first one, query "Full Text Articles" post, filter by relationship "author-fulltextarticle-author"
- The second one, query "Web Exclusive Articles" post, filter by relationship "author-webexclusive-author"

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.