Skip Navigation

[Gelöst] How to filter parent posts by user

This support ticket is created vor 7 Jahre, 6 Monate. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Dieses Thema enthält 18 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Shane vor 7 Jahre, 6 Monate.

Assistiert von: Shane.

Author
Artikel
#453073

I created a parent / child relationship between the content type "Stock" (father) and the content type "Issue" (child). The Stocks have products.

The users can create issues with a CRED form, related with their product through the select relationship field. Currently it shows all the items from the father, but I need to filter this result, showing only those corresponding to the user logged.

How could I do it?

Thank you.

#453170

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Carlos,

Thank you for contacting our support forum.

So you want to filter the posts for the current logged in user correct?

What you need to do is add a Post Author query filter and set it to display for those users who are currently logged in.

Thanks,
Shane

#453390
Captura de pantalla 2016-11-04 a las 13.02.35.png

Yes, I need to filter these posts but inside the select field who displays the relationship. In this form it's named 'Assigned resources'
I do not know if I'm explaining well.

#453442

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Carlos,

So you essentially want the select field to only display the posts of the current user ?

Thanks,
Shane

#453784

Yes, this is why we're working in an reporting system where the users can send us notifications by e-mail about a problem with their equipment. To make it more ease for the user, we need to display only the equipment assigned to him in the select field.
Is it possible?
Thank you in advance.

#454475

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Carlos,

Unfortunately no this would not be possible.

It would require some custom coding in order to achieve. Such custom coding is out of the scope of our support forum.

However you can get further assistance on this by contacting one of our certified consultants by going to the link below.
https://toolset.com/consultant/

Thanks,
Shane

#455909

Hello Shane, I have created this view with a user filter to display all the products assigned to the current user, it works fine:

[wpv-view name="items-in-selection-when-creating-an-issue" users='[wpv-current-user info="id"]']

Now, I want to display these results (JSON) inside the select field. I am using this code but it doesn't work, the select field don't appears on the screen:

      
[cred_generic_field field="assigned-resources3" type="select" class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":[],
"options":[[wpv-view name="items-in-selection-when-creating-an-issue" users='[wpv-current-user info="id"]']]
}
[/cred_generic_field]  

If I take one of the results of the view manually, and put it inside de "options", the select appears:

"options": {"value":"180","label":"wegf"}

Can you check the code in case I left anything?

Thank you in advance.

#456124

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Carlos,

Can you create a testing page and send me the link ?

On that testing page I need for you to add this view to it [wpv-view name="items-in-selection-when-creating-an-issue" users='[wpv-current-user info="id"]']

I want to see if the html divs are rendered around the view.

Thanks,
Shane

#456745

Hello Shane, here is the link to the page:

versteckter Link

You will see a JSON result at the end. The select must be displayed below the Assigned resources 3 field.

The site is protected by .htpasswd, these are the credentials:

U: sistemas
P: kec2016

Tell me if you need back-end access. In this case, how can I send you this info?

Thank you.

#457409

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Carlos,

We actually have a custom solution for this 🙂 however we won't be able to provide much debugging on this since its custom code.

Add the following to your CRED form.

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

Add this to the js editor of your CRED form.

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

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');

Add that to your functions.php file.

This should display the parent posts for the current user in your CRED form.

Thanks,
Shane

#457613
Captura de pantalla 2016-11-16 a las 12.25.49.png

Hello Shane, thank you very much for your help, We're very thankful.

The code works correctly, except for the tag [get-parents] indicated in the value of the input field. For some reason it is not executing it and the value returns the tag as it is.

If we insert the id's manually, separated by commas, the select shows the results.

Any idea?

#457766

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Carlos,

That is strange as the shortcode should render normally.

Did you add this to your functions.php file?


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');


Thats the most immediate issue I can think of.

Thanks,
Shane

#457770

Hi Shane, of course, the function was added.

If we put the shortcode out of the input field, it works fine. Look at this line:

<input type="hidden" id="parents_id" value="[get-parents]" /> This is only for test display: [get-parents]

The shortcode inside the value label don't work, but the one outside it does and returns the correct results 🙁

Have you ever encountered a similar problem?

#457780

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Carlos,

Could you try using views to display the form ?

Meaning add the form to a view you created or a content template .

Also Add the shortcode name to views compatibility by going to Toolset -> Settings -> Frontend content under the Third-party shortcode arguments

Please try this and let me know if it helps.

Thanks,
Shane

#457907

That was the problem!!!! Now it works fine 🙂
Thank you very much for your help. Excellent support.

Dieses Ticket ist jetzt geschlossen. Wenn Sie ein Toolset Kunde sind und Hilfe benötigen, eröffnen Sie bitte ein neues Support-Ticket.