Skip Navigation

[Resolved] I want to only display the parent items of the current user in a select box

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

Problem:
How to show parent posts in the CRED parent Picker field, that are authored by the current logged in user only

Solution:
1. A "Create [child] Post Form"

2. In the Parent Selector Code of that form you should use only default available arguments, as this:

<div class="cred-group cred-group-parents">
  <div class="cred-field cred-field-_wpcf_belongs_page_id">//change "page" to your parent post slug
    <label class="cred-label">
      Choose Parent
    </label>
     This is only for test display: [get-parents]
      [cred_field field='_wpcf_belongs_page_id' value='']//change "page" to your Parent Post Slug
  </div>
</div>

3. In the JS:

jQuery('document').ready(function(){
    
var post_parents = jQuery('#parents_id').val();
    
var arr = post_parents.split(',');
      
  jQuery("[name=_wpcf_belongs_page_id] > option").each(function() { //change "page" to your parent post slug
        
    var option_val = jQuery(this).val();
        
     if( jQuery.inArray(option_val, arr) == -1 && option_val != -1 ){
       jQuery(this).remove();
     }
  });
      
});

4. In functions.php:

function get_parents($atts) {
    global $current_user;
    get_currentuserinfo();
    $author_query = array('post_type' => 'page', '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');

5. in Views > Compatibility > 3rd party ShortCodes register:

get-parents

6. If you insert the CRED Form in a Post, it correctly displays only parent Posts of current author/user.

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

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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 2 replies, has 2 voices.

Last updated by garyF-3 7 years, 1 month ago.

Assisted by: Beda.

Author
Posts
#570061

Tell us what you are trying to do?

I have a custom post type called contact. When someone creates a contact I want to provide a dropdown of companies (parent post type) that they have already created. From this list I want them to be able to select one. This then becomes the parent post that the cred form will assign to the child contact post.

Is there any documentation that you are following?
https://toolset.com/documentation/user-guides/conditional-display-for-form-inputs/
https://toolset.com/forums/topic/cred-parent-child-drop-down-relationship/

What is the link to your site?
hidden link

#570084

I think, if you follow this Thread, you can achieve exactly what you want:
https://toolset.com/forums/topic/i-want-to-only-display-the-parent-items-of-the-current-user-in-a-select-box-2/

Please let me know if that helps!

#572064

Sorted this out