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
)
);
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
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?
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"