I tried the code exactly as you wrote:
$oeuvres = get_posts( array(
'post_type' => 'oeuvre',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids'
) );
$cocktails = toolset_get_related_posts(
array( 'parent' => $oeuvres ),
'oeuvre-cocktail',
array(
'return' => 'post_object',
'role_to_return' => 'child'
)
);
$ids = wp_list_pluck( $cocktails, 'ID' );
$query->set( 'post__in', $ids);
Gives no result (and crashes Elementor)
But when I try:
(replacing $oeuvres with right IDs)
$oeuvres = array('187','159');
$cocktails = toolset_get_related_posts(
array( 'parent' => $oeuvres ),
'oeuvre-cocktail',
array(
'return' => 'post_object',
'role_to_return' => 'child'
)
);
$ids = wp_list_pluck( $cocktails, 'ID' );
$query->set( 'post__in', $ids);
It works!
So I tried to convert the IDs into arrays with single quotes.
Something like:
foreach ( $lesoeuvres as $uneoeuvre )
{
$links[] = $uneoeuvre->ID;
}
$yes2 = "'" . implode ( "', '", $links ) . "'";
(or $yes2 = "'".implode(',',$links)."'";)
but it does not work.
How to get in var $oeuvres the IDs of cocktails, but separated by a comma, with single quotes, THEN pass it to array( 'parent' => $oeuvres )
I don't think the issue is within the code that we shared, we can use it to restrict the posts on a Toolset view. Check the test server on this page hidden link
It only returns the linked cocktails, even if the view is configured to return all cocktails, because we use the following custom code in Toolset->Settings->Custom code, which is similar to adding it to functions.php.
add_filter( 'wpv_filter_query', 'prefix_get_cocktails_linked_to_an_oeuvre', 30, 3 );
function prefix_get_cocktails_linked_to_an_oeuvre( $query_args, $view_settings, $view_id ) {
if ( 35 == $view_id ) {
$oeuvres = get_posts( array(
'post_type' => 'oeuvre',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids'
) );
$cocktails = toolset_get_related_posts(
array( 'parent' => $oeuvres ),
'oeuvre-cocktail',
array(
'return' => 'post_object',
'role_to_return' => 'child'
)
);
$ids = wp_list_pluck( $cocktails, 'ID' );
$query_args['post__in'] = $ids;
}
return $query_args;
}
But, if you want to try to change $oeuvres so it holds an array of IDs between single quotes, try the following code:
$oeuvres_as_strings = array();
foreach ( $oeuvres as $oeuvre )
{
$oeuvres_as_strings [] = (string) $oeuvre;
}
And if you want the IDs of $cocktails as a string separated by a comma, try this:
$ids = wp_list_pluck( $cocktails, 'ID' );
$ids_as_a_string = implode( ',', $ids );
I do not understand this part:
we use the following custom code in Toolset->Settings->Custom code
I have never used this. How to use this?
Does it create a shortcode?
I went to the demo website, I copied the custom code to my demo site,
but then I don't know what to do with it,
since I don't use Blocks.
Where does it fit in the php function?
This code crashes Elementor:
$oeuvres = get_posts( array(
'post_type' => 'oeuvre',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids'
) );
$oeuvres_as_strings = array();
foreach ( $oeuvres as $oeuvre )
{
$oeuvres_as_strings[] = $oeuvre->ID;
}
$cocktails = toolset_get_related_posts(
array( 'parent' => $oeuvres ),
'oeuvre-cocktail',
array(
'return' => 'post_object',
'role_to_return' => 'child'
)
);
$ids = wp_list_pluck( $cocktails, 'ID' );
$ids_as_a_string = implode( ',', $ids );
$query->set( 'post__in', $ids_as_a_string);
we use the following custom code in Toolset->Settings->Custom code
I have never used this. How to use this?
Does it create a shortcode?
This is our alternative to editing directly the functions.php file and adding custom code. The custom code can be for a shortcode or for a hook just like yours.
The last code that I provided in my reply #1787625 does return the list of Cocktails posts that are linked to any Oeuvre post. And my reply #1787911 demonstrates how to use it inside a hook. So, I don't see why we'll need to convert the IDs to string and pass it to "toolset_get_related_posts" if it is already working with numeric IDs.
I, previously, understood that $query->set( 'post__in', $ids) is how Elementor allow to hook into its lists or queries. But that's something you should check with the Elementor team.
So, regarding your last code, I'll see that you need to debug it in order to understand what's going on. I'll suggest using ways to check the progress of the code using error_log function, or using a debugging solution such as Xdebug.
// the following line will write the content of the $posts object to the debug.log file, in a readable way.
error_log( print_r( $posts, true ) );
- hidden link
- hidden link
- hidden link