Skip Navigation

[Resolved] Current user posts in child posts

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

Problem:
Show parents posts title as dropdown options with parent select box belongs to current user with CRED post form.

Solution:
To display the parent post title belongs to current user you need to add some custom code. The proposed solution you can find at:
=> https://toolset.com/forums/topic/current-user-posts-in-child-posts/#post-581569

Relevant Documentation:

This support ticket is created 7 years 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 christopherH-7 7 years ago.

Assisted by: Minesh.

Author
Posts
#581477
Screenshot_4.png

Tell us what you are trying to do?
I want to show current users posts in child posts select box in Cred Post form

Is there any documentation that you are following?
I have created parent child relationship by toolset documentation and its working fine but I need help on other topic.

Is there a similar example that we can see?
For example I have showed child posts in select box like [cred_field field='_wpcf_belongs_certification_id' value='' select_text='--- not set ---' class='form-control' output='bootstrap' wpv-post-author format='meta' meta='ID'] in form. Its showing all the custom posts but I want to show only current users posts in select box.

What is the link to your site?
hidden link

#581569

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - to display only current user parents:

1) - add following hidden field just before your parent dropdown shortcode:
For example:

<input type="hidden" id="parents_id" value="[get-parents]" /> 
[cred_field field='_wpcf_belongs_certification_id' value='' select_text='--- not set ---' class='form-control' output='bootstrap'] 

2)
Add [get_parents] shortcode to your current theme's functions.php file. Please add following code to your functions.php file:

function get_parents($atts) {
    global $current_user;
    get_currentuserinfo();
    $author_query = array('post_type' => 'certification', 'posts_per_page' => '-1','author' => $current_user->ID,);
    $author_posts = new WP_Query($author_query);
    $parent_ids = "";
    while($author_posts->have_posts()) : $author_posts->the_post();
        $parent_ids .= get_the_ID() .",";
    endwhile;
     
    return $parent_ids;
}
add_shortcode('get-parents', 'get_parents');

3)
In your CRED form's JS box, try to add following code:

jQuery('document').ready(function(){
     
var post_parents = jQuery('#parents_id').val();
     
var arr = post_parents.split(',');
       
  jQuery("[name=_wpcf_belongs_certification_id] > option").each(function() {
         
    var option_val = jQuery(this).val();
         
     if( jQuery.inArray(option_val, arr) == -1 && option_val != -1 ){
       jQuery(this).remove();
     }
  });
       
});

I hope above steps will help you to resolve your issue.

#581579
Screenshot_5.png

Hi Minesh,

Thanks for the prompt response but its not working at my end. I have added the function in theme's functions.php and jQuery and hidden input fields as well in toolset form as mentioned. Users Posts are not updating in hidden field. Please check attached screenshot for more clarification. Please let me know if you need any details from my side?

#581582

Minesh
Supporter

Languages: English (English )

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

I see the shortcode [get-parents] is not parsed.

Could you please try to register shortcode "get-parents" at and check if that help you to resolve your issue:
=> Toolset => Settings => Frontend content Tab => Third-party shortcode arguments

#581592

Thank you! Its working fine now.