I am trying to:
Perform a query within a custom function in functions.php. It appears as though the toolset_relationships part of the query is ignored.
$args = array(
'post_type' => 'fast-fact',
'post_status' => 'publish',
'numberposts' => -1,
'toolset_relationships' => array(
'role' => 'child',
'related_to' => $post->ID,
'relationship' => 'destination_fast-fact'
)
);
Link to a page where the issue can be seen:
No page, this is local on my laptop.
I expected to see:
I expect it to limit the posts to just those that are a child of the post.
Instead, I got:
It returns all child posts, not just the ones for that post.
Hi, let's confirm these things first:
- Check the remarks section here for information about timing and when the toolset_relationships query is available in the request lifecycle: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#remarks
- If you have not already, echo or log $post->ID just before this $args array and confirm it is the numeric ID of a publicly published "destination" post.
- Double-check the relationship slug. You can inspect it in Toolset > Relationships.
- Double-check the parent and child aliases. When editing the Toolset Relationship, check the confirmation checkbox and you'll be shown which post is parent and which is child. Looks like Fast-fact should be the child and Destination should be the parent.
- Update to the latest version of Types, then temporarily deactivate all other plugins.
- Test again. If the filter works, reactivate your plugins one by one until the problem returns.
- If the filter still does not seem to work, I'll have to take a closer look.
Hi Christian,
I'm guessing it had something to do with the lifecycle. This query works when I use it within the context of a page to display posts, but when I try to reference it in my functions.php, it doesn't seem to register.
So to be specific, I'm trying to add extra meta info from child posts to include in the search index of SearchWP.
lien caché
And here's a better example of how I am doing it:
lien caché
Here's how I'm doing it...
function my_searchwp_extra_metadata($extra_meta, $post_being_indexed) {
if ( 'destination' !== get_post_type( $post_being_indexed ) ) {
return $extra_meta;
}
$args = array(
'post_type' => 'fast-fact',
'post_status' => 'publish',
'numberposts' => -1,
'posts_per_page' => 150,
'suppress_filters' => true,
'toolset_relationships' => array(
'role' => 'child',
'related_to' => $post_being_indexed->ID,
'relationship' => 'destination_fast-fact'
)
);
$destination_posts = get_posts($args);
if (! empty($destination_posts)) {
$extra_meta['fast_facts'] = array();
foreach ($destination_posts as $destination_post) {
$post_meta = get_post_meta( absint( $destination_post->ID ), 'wpcf-fact-description', true );
$extra_meta['fast_facts'][] = $post_meta;
}
}
return $extra_meta;
}
add_filter('searchwp_extra_metadata', 'my_searchwp_extra_metadata', 10, 2);
So I guess the big question is...how do I retrieve child posts within this context using Toolset?
I've updated the code to this and it seems to work, but I'm not sure I can make sense of it.
function my_searchwp_extra_metadata($extra_meta, $post_being_indexed) {
// we only care about WooCommerce Products
if ( 'destination' !== get_post_type( $post_being_indexed ) ) {
return $extra_meta;
}
$fast_facts = toolset_get_related_posts (
$post_being_indexed->ID,
'destination_fast-fact',
'parent',
100000,
0,
array(),
'post_object',
'child'
);
if (! empty($fast_facts)) {
$extra_meta['fast_facts'] = array();
foreach ($fast_facts as $fast_fact) {
$post_meta = get_post_meta( absint( $fast_fact->ID ), 'wpcf-fact-description', true );
$extra_meta['fast_facts'][] = $post_meta;
}
}
return $extra_meta;
}
add_filter('searchwp_extra_metadata', 'my_searchwp_extra_metadata', 10, 2);
Does the query make sense?
Yes it looks pretty good to me, If you're only using the post ID there's no reason to return the full post_object in the toolset_get_related_posts call. You can replace "post_object" with "post_id", and then modify the get_post_meta call like so:
$post_meta = get_post_meta( absint( $fast_fact ), 'wpcf-fact-description', true );
But as it is now is fine and probably not noticeably slower.
Thanks for confirming. I went ahead and made that change and all works!