Skip Navigation

[Resolved] Restrict Parent picker in CRED to posts authored by logged in user

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

Problem:
How can I show a list of posts in the CRED parent Selector that belong to the current logged in user only?

Solution:
It requires Custom Code, that is not supported by Toolset.
https://toolset.com/forums/topic/cred-field-showing-too-many-option-in-form-select-box/#post-425033

This support ticket is created 7 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
- - 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 3 replies, has 3 voices.

Last updated by tinaH 7 years, 8 months ago.

Assisted by: Beda.

Author
Posts
#424946
Screen Shot 2016-08-15 at 8.28.33 PM.png
Screen Shot 2016-08-15 at 8.29.14 PM.png

I am trying to: allow users to choose a collection for their x's where collection is a parent of x's.

I visited this URL: hidden link

I expected to see: only collections that I created, not collections that other users have created.

Instead, I got: all collections available instead of just mine....

Basically, i have a cpt of collections that each user can create. I then have x's (also a cpt) that can be added to any of that user's collections. In the auto generated form, for collection, it shows me ALL collections, not just those authored by the current user. I need to be able to limit those options to only those that were created by the current user. Sort of like photos....I shouldn't be able to add photos to another user's album...

Attached Images:
If there is a way to limit the highlighted cred field in the highlighted code section, then it wouldn't show all the options in the select box on the form...for instance, GNO was created by a another user...

Happy to provide Admin access to my site as it's closed to search engines now while in development....

Thanks!

#425033

Do I understand correctly, that you have a CRED form that creates a Child Post to a Parent post, and want to restrict the DropDown that lists the available Parents, to the ones belonging to a certain author only?

This should help you:
https://toolset.com/forums/topic/i-want-to-only-display-the-parent-items-of-the-current-user-in-a-select-box-2/#post-351830

To sum up:

1. By default CRED parent Picker lets you choose from all available parent Posts.

2. This snippet lets you populate the picker with only the parent posts of the current logged in user

3. This is how the CRED parent Picker field HTML has to look:

<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>
    <input type="hidden" id="parents_id" value="[get-parents]" /> 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>

4. This is the JS that has to be inserted to the CRED JS Editor

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();
     }
  });
     
});

5. And this is the function to put into functions.php of your Theme

function get_parents($atts) {
    global $current_user;
    get_currentuserinfo();
    $author_query = array('post_type' => 'page', 'posts_per_page' => '-1','author' => $current_user->ID,); //change page to your parent post slug 
    $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');

6. It is recommended to use Views with this and register the ShortCode in Views > Compatibility

Please aknowledge that this is custom programming work which is beyond the scope of our support.

If you need more complex Custom Logic, I would suggest you consider contacting one of our certified partners from this link:
https://toolset.com/consultant/

You will get the custom assistance you need to get on with your project.

#425237

Wow, what fantastic help! This is EXACTLY what i needed. Everything worked as described. This was very helpful...been working on solving this for days and finally just threw in the towel and asked for help. Thanks again!!

#480733

OMG!
I wish I understood how this works - but it does!
So happy I found this ticket ...
Pin it - big for everyone to find ...

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.