[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.
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.
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.
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;
}