I have a custom post type called "art"
I have a custom post type called "cocktails"
They are in a relashionship, where one art can inspire many cocktails
On a wordpress page, I want to put a View where you can see all cocktails... That have an art (any art) as a parent.
All cocktails inpired by art. Just the cocktails (not the art).
NOT ALL cocktails are inspired by art: only a few.
Plus, this is not in a Single page or a loop: just an index page if you will, where you see all cocktails, separated in different sections, one section being ''cocktails inpired by art''.
What I am looking for: How to write the query in functions.php to show cocktails inspired by art.
I read the toolset_get_related_posts documentation, but it only addresses the case where there is a parent ID or a children ID, but I am not in a single page, so I don't have any.
Hello,
There isn't such a built-in feature within Toolset, in your case, you can consider a nested query:
1) Query all parent "art" posts
https://developer.wordpress.org/reference/classes/wp_query/
2) In the query loop, query related child "cocktails" posts:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
And return the "cocktails" posts IDs in an PHP array, pass the result to below query
3) Query all query "cocktails" posts, filter by post IDs in above PHP array result:
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters
post__in (array) – use post ids.
Then you will be able to get all "cocktails" posts which have parent "art" post setting
Thank you Luo for your answer
I had deducted this, however it does not work.
This is the code so far:
// show cocktails that have a parent art
function my_custom_query9( $query, $widget ) {
$args = array(
'post_type' => 'art',
'fields' => 'ids'
); ----------------- HERE1
$cocktail_id6 = toolset_get_related_posts(
$args, // ids of all art -------------------- HERE2
'art-cocktail', // relashionship
'parent',
1000000, 0,
array( ),
'post_id',
'child'
);
$query->set( 'post__in', $cocktail_id6);
}
add_action( 'elementor/query/maquery9', 'my_custom_query9', 10, 2 );
HERE1: Will this give me the Ids of all existing art? In the form of an array?
HERE2: How to turn this into an array?
Is this the right place to put it?
Thanks for any pointers
I assume you are going to display a view in a widget, there is a workaround without custom codes, you can try these:
1) Create a post view "child-cocktail-posts":
- Query cocktail posts
- Filter by relationship filter:
Select posts in a arts cocktails relationship that are a related to the current post in the loop.
- In view's loop, display cocktail post information
2) Create a post view "parent-art-posts"
- Query art posts
- In view's loop, display post view "child-cocktail-posts" shortcode:
[wpv-view name="child-cocktail-posts"]
3) In your widget, display the post view "parent-art-posts" shortcode:
[wpv-view name="parent-art-posts"]