Skip Navigation

[Resolved] u_post_count function of children posts

This thread is resolved. Here is a description of the problem and solution.

Problem:
u_post_count function of children posts - get related posts in relationship based on provided author ID

Solution:
To query the related posts based on the current loggedin user or author, you will require to use the WP_Query to fetch the related posts in relationship based on provided author.

You can find proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/u_post_count-function-of-children-posts/#post-2067581

Relevant Documentation:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

This support ticket is created 3 years, 7 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by annaA-2 3 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#2067217

Hi,
I'm trying to create a voting system and I followed Beda's instructions:
https://toolset.com/forums/topic/voting-system/

Then I followed other documentations to limit the post count function to children post of a parent. Users can vote one time for a parent post but they can vote for other parents.

I tyed this code but it doesn't work:

function u_post_count() {
$user_post_count = count( get_posts( array(
'post_type' => 'valutazione-sogg',
'posts_per_page' => -1,
'author' => get_current_user_id(),
'toolset_relationships' => array(
array(
'role' => 'child',
'related_to' => get_the_ID(),
'relationship' => array( 'politico', 'valutazione-sogg' )
)),
) ) );
return $user_post_count;
}

I can't connect parents with their children's posts. What am I doing wrong?
Thank you for your help.

Best.

#2067395

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please tell me where you added the code that connects the posts and what is the post-relationship you created?

Toolset do not offer the voting system natively but you will have to write your custom code to build it.

#2067417

Hi Minesh,
thank you for your answer.

I followed this forum post: https://toolset.com/forums/topic/voting-system/ to create a voting system.

Then I followed Beda instructions to limit post submission https://toolset.com/forums/topic/logged-in-users-can-submit-the-form-and-create-content-only-one-time/

So I added my code into Toolset -> Settings -> Custom code and then I added u_post_count function to Toolset > Settings > Front End Content > Functions inside conditional evaluations.

After I used an HTML Conditional in my Content Template to show create post form if the value is 1.

It works fine, but after a post-submission user can't vote for other parents' posts. I need to limit child post submission in relation to its parent's post.

My relationship is Politici - Valutazioni soggettive (slug: politico-valutazione-soggettiva) where Politico is parent and Valutazioni soggettive is Child. It s a one to many relationship.

But I can't link them in my function and I don't understand what I am doing wrong.

I hope my explanation is clear enough 🙂
Thanks for your help.

#2067581

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To query the related posts based on the current loggedin user, you will require to adjust the u_post_count() function code as given under:

function u_post_count() {
    global $post;

 $query = new WP_Query( 
 array(
  'post_type' => 'valutazioni-soggettive',
  'posts_per_page' => -1,
   'author'    => get_current_user_id(), 
  //new toolset_relationships query argument
  'toolset_relationships' => array(
   'role' => 'child',
   'related_to' => $post->ID,
   // this will work only with relationships that have existed before the migration
   // if possible, use the relationship slug instead of an array
   'relationship' => 'politico-valutazione-soggettiva'
  ) ) );
$user_post_count = count($query->posts);

return $user_post_count;
}

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

Can you please adjust the code and check if that helps you to resolve your issue.

#2067961

Hi Minesh,
Your code works fine, I changed the post slug, and now it is perfect!

Thank you so much!