Skip Navigation

[Resolved] Unable to filter post by post status with toolset_get_related_post

This support ticket is created 3 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 8 replies, has 2 voices.

Last updated by himanshuS 3 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#1979181

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?

#1979261

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

#1979905

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.

#1980681

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.

#1980869

Just wanted to update you that the information about the "toolset_get_related_post" function has been fixed:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

#1981427

Waqar,

Thank you. Now, I have to update my custom code across 100 different scripts 🙁

#1981429

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?

#1981923

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.

#1983131

My issue is resolved now. Thank you!