I have used this article (https://toolset.com/forums/topic/toolset_get_related_post-question/) but still cannot filter the output by post status. The code still returns 2 ids when it should just return 1.
I only have two post: 1 published and other not publised.
The code I am using is as below:
<?php global $wpdb;
global $post;
$current_post_type = get_post_type( );
$user_id = get_current_user_id();
if($current_post_type == 'portfolio' ) {
$user_id = $post->post_author;
}
$args = array(
'author' => $user_id,
'post_type' => 'endorsement-request',
'post_status' => 'publish'
);
//$test_post = get_post(13580);
//print_r($test_post);
$count =0;
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
// Do Stuff
$post_id_er = get_the_id();
//echo $post_id_er;
//echo 'break';
$endorsement_id = toolset_get_related_post($post_id_er, 'endorsement-request-to-endorsement', 'child', 'draft');
If ($endorsement_id!=0)
{
$count = $count +1;
// echo $endorsement_id;
// echo 'break';
}
endwhile;
endif;
echo $count;
// Reset Post Data
wp_reset_postdata();
?>
The code returns the same value (2) for:
$endorsement_id = toolset_get_related_post($post_id_er, 'endorsement-request-to-endorsement', 'child', 'draft');
$endorsement_id = toolset_get_related_post($post_id_er, 'endorsement-request-to-endorsement', 'child', 'publish');
$endorsement_id = toolset_get_related_post($post_id_er, 'endorsement-request-to-endorsement', 'child', array(publish));
It almost seems like the function is ignoring the value entered is returning all the posts.
Video link here: hidden link
What could I be missing?
Hi,
Thank you for contacting us and I'd be happy to assist.
For cases where more than one related results are available, it is better to use the "toolset_get_related_posts", instead of "toolset_get_related_post":
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
On my test website this works:
// to get the published related post
$endorsement_id = toolset_get_related_posts( $post_id_er, 'endorsement-request-to-endorsement', 'parent', 1, 0, array('post_status' => 'publish'), 'post_id', 'child');
// to get the draft related post
$endorsement_id = toolset_get_related_posts( $post_id_er, 'endorsement-request-to-endorsement', 'parent', 1, 0, array('post_status' => 'draft'), 'post_id', 'child');
regards,
Waqar
The post is in an O2O relationship so toolset_get_related_post would work.
This worked and it means that the above-mentioned article is wrong and overall documentation is also wrong.
'publish' or 'draft' or 'publish,draft' does not work as an input for post_status with toolset_get_related_post function.
Please correct me if I am wrong.
Thanks for writing back and your observation is correct.
The documentation for the “toolset_get_related_post” function suggests that to filter the related result by post status, its array should be provided as the 5th parameter:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
Example:
$result_id = toolset_get_related_post( $post_id, 'shop-book', 'child', $args, array('draft') );
But my tests have shown that it doesn't work that way and you actually have to provide the allowed statuses array as part of the “arg” array, which is the 4th parameter.
( which is also consistent with how it works with the “toolset_get_related_posts” function )
Example:
$result_id = toolset_get_related_post( $post_id, 'shop-book', 'child', array('post_status' => 'draft') );
Thank you for bringing this forward and I've shared these findings with the concerned team for further review.
Waqar,
Thank you. Now, I have to update my custom code across 100 different scripts 🙁
Thanks Waqar. Now I have to update the code for toolse_get_related_post in 100 different scripts manually 🙁
Is there a better way to change the code in my custom scripts?
Unfortunately, I can't think of any automated way of bulk updating the code in the custom scripts.
If you're comfortable with the database/SQL changes, something like this might be possible through complex search and replace, but, that option should only be tried after making a complete backup of the database.
My issue is resolved now. Thank you!