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>
    <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>

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 8 years, 4 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 10 replies, has 3 voices.

Last updated by razvanP 8 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#351411
314713-screenshot2.jpg
314713-screenshot1.jpg

I am trying to: limit the parent options in a select box on a CRED from by current user
I visited this URL: hidden link
I expected to see: a limited list of projects to choose from
Instead, I got: the entire list of projects created by all users

Basically I have a custom type called "Application" that is a child of "Project". In the CRED form I am using

[cred_field field="_wpcf_belongs_project_id" author="[wpv-post-author format='meta' meta='ID']" value="" max_results="0" order="date" ordering="desc" required="true" select_text="--- Select project ---" validate_text="project must be selected"]

to display a project for selection. I need to limit the projects that display so that only the projects that have been created by the current user are able to be selected.

I can do this in views using the "Select posts with the author the same as the current logged in user" filter, but the same functionality doesn't seem to be available for the cred_field parameters.

Thanks
J

P.S. This topic was discussed here, but was marked as resolved when it wasn't resolved: https://toolset.com/forums/topic/i-want-to-only-display-the-parent-items-of-the-current-user-in-a-select-box/

#351455

Thank you for contacting us here in the Support Forum

I've read through the previous 2 threads, hope I dint miss anything.

I am confused why you use "author="[wpv-post-author format='meta' meta='ID']""

CRED parent picker fields don't have a argument for the author.

Unless you midfield the CRED behavior, this won't work.

From the other thread I understand functionality worked until the WordPress API Update for ShortCodes back in time.
This is a bit strange because as far I see you do use JS to achieve this goal and that was not affected by the update, just nested ShortCodes and ShortCodes as HTML attributes.

Anyway, would you like to send me a detailed description of the exact code you use (including JS and PHP from functions.php, in case) so I can debug this?

I would quickly deeply a system locally and apply your approach so to see what's wrong.

Thank you for your patience.

#351622

Hi,

Thanks for reading through everything. Here is the code for the page:

<div class="cred-group cred-group-parents">
		<div class="cred-field cred-field-_wpcf_belongs_project_id">
		<label class="cred-label">Select the primary project/film for this application</label>
		<input type="hidden" id="parents_id" value="[get_parents]" />[get_parents]
		[cred_field field="_wpcf_belongs_project_id" required="true" select_text="--- Select project ---" validate_text="Project must be selected. Make sure you have created a project above."]
		</div>
	</div>

This is the javascript:

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

And this is the code in the function.php

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

Hope that helps.
James

#351830

[get_parents] is returning correctly only the Current Author (users) posts on my end.

But simultaneously I get a notice:

Notice: Undefined index: input in /Applications/MAMP/htdocs/ToolsetSupp/wp-includes/kses.php on line 863

Are you seeing hat notice to, after you turn on WP Debug on your site?

I was able to fix this by changing the ShortCode to [get-parents] and registering it under Views > Compatibility > 3rd Party ShortCodes.

This also works great on my front end.

I used the same code as you do, here again the setup process:

1. A "Create [child] Post Form"

2. In the Parent Selector Code of that form you should use only default available arguments, in my case it looks like 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>
    <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>

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 I insert the CRED Form in a Post, it correctly displays me only parent Posts of current author/user.

Please don't hesitate to inform me in case the issue persists

Thank you for your patience.

#354422
Screen Shot 2015-12-17 at 8.15.50 PM.png

I think we are getting somewhere 🙂
I have implemented the JS (copy and paste)
I have also implemeted the function code as you said (with the exception of changing the post type => page to post type => project).
Here is my current code in the form:

<div class="cred-group cred-group-parents">
		<div class="cred-field cred-field-_wpcf_belongs_project_id">
		<label class="cred-label">Select the primary project/film for this application</label>
		<input type="hidden" id="parents_id" value="[get_parents]" />[get_parents]
		[cred_field field="_wpcf_belongs_project_id" required="true" select_text="--- Select project ---" validate_text="Project must be selected. Make sure you have created a project above."]
		</div>
	</div>

The get_parent shortcode is pulling through the correct info (see screenshot) but for some reason the select box is still displaying all the projects and not just the one attributed to the current user?

#354683

Do you have also edited the JS accordingly?

Note that I commented inline in all codes where to change what.

Please also make sure "project" is the SLUG of your Post type.

If this still doesn't work, check if the issue also persist with a WordPress Default Theme and NO Plugins BUT the Toolset Plugins?

If not, could you then re-enable the Plugins one after the other, and check the issue each time you enable a plugin?
Please report me when the issue comes back
It might also be due to the Theme.
Please do reactivate your Theme only after you are sure the issue isn't coming form a 3rd Party Plugin.

If that doesn't help, I would need to request temporary access (WP-Admin and FTP) to your site
- preferably to a test site where the problem has been replicated if possible -
in order to be of better help and check if some configurations might need to be changed

Your next answer will be private which means only you and I have access to it.

❌ Please backup your database and website ❌

✙ I would, if possible, need access to a site where only a minimal set of Plugins and a default theme is active.
This to avoid eventual compatibility issues with other software.

✙ Please add the Links to:

- The CRED Edit Screen

- The Page/Post where you insert the CRED Form

- The corresponding Front End Page/Screen

Please do not hesitate to open a new thread if other issues or problems arise

Thank you for your patience.

#358811

Thanks for the Details

✙ Please add the Links to:

- The CRED Edit Screen

- The Page/Post where you insert the CRED Form

- The corresponding Front End Page/Screen

- I need access to a Profile of a user that actually authored one or more specific Parent Posts

- Please also add links to those parent posts

Thank you for your collaboration.

#358815

Cred edit screen:
hidden link

Page with cred form (bottom left + Add new application toggle):
hidden link

The corresponding Front End Page/Screen
Same as above (hidden link)

I need access to a Profile of a user that actually authored one or more specific Parent Posts
I have created a test project for your profile (username: types)

Please also add links to those parent posts:
hidden link
(don't have a view configured for single projects)

Thanks again

#358865

1. You used the wrong syntax for registering the ShortCode in Views Settings.
You used the functions name instead of the ShortCode's name.
I adjusted that.

2. Then I edited your CRED Form HTML to test the behavior and it is working fine for this user you created for me.

You can remove the parts:
- This is only for test display: [get-parents]
- //change "page" to your Parent Post Slug

3. Style the rest as you need, the fictionality is working fine now.

Please do not hesitate to open a new thread if other issues or problems arise

Thank you for your patience.

#359129

Thank you so much! It's ALWAYS something small that I miss 🙁
I really appreciate you help with this!!!
Keep up the great work 🙂

#620788

Hi Guys,

I followed all the steps, but I still have the full list of post instead of just author's posts.
Is the structure or function of the fields changed since the last post? Could you please help?

Please find below the 3 blocks of code that I'm using:

 
<div class="cred-group cred-group-parents">
  <div class="cred-field cred-field-_wpcf_belongs_site_id">
    <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_site_id' value='']
  </div>
</div>
 
jQuery('document').ready(function(){
    
var post_parents = jQuery('#parents_id').val();
    
var arr = post_parents.split(',');
      
  jQuery("[name=_wpcf_belongs_site_id] > option").each(function() { 
        
    var option_val = jQuery(this).val();
        
     if( jQuery.inArray(option_val, arr) == -1 && option_val != -1 ){
       jQuery(this).remove();
     }
  });
      
});
 
 
function get_parents($atts) {
    global $current_user;
    get_currentuserinfo();
    $author_query = array('post_type' => 'site', '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'); 

Thanks!
R

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