Skip Navigation

[Resolved] Filter the options in Post Relationship field in wp-admin post editor

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

Problem: I would like to filter the options shown in the Post Relationship field in the post editor screen. Only the current User's posts should be shown here.

Solution: There is not a good way to do this in wp-admin, but you can use generic fields in CRED to accomplish something similar.
- Create a CRED form to add posts and insert a generic select field
- Create a View of posts, filtered by author, where the author is the current User
- In the Loop Output editor, insert this code to replace the default wpv-loop tags:

<wpv-loop>
      [wpv-item index=1]
        {"value":"[wpv-post-id]","label":"[wpv-post-title]"}
      [wpv-item index=other]
      ,{"value":"[wpv-post-id]","label":"[wpv-post-title]"}
</wpv-loop>

- Add the following custom code to your functions.php file:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
 
function prefix_clean_view_output( $out, $id ) {
  $ids = array( 12345 );
  if ( in_array( $id, $ids )) {
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}

add_action('cred_save_data', 'cred_belongs_impresa_action',10,2);
function cred_belongs_impresa_action($post_id, $form_data)
{
    $forms = array( 67890, 98765 );
    // if a specific form
    if (in_array($form_data['id'], $forms))
    {
        if (isset($_POST['selectcompany']))
        {
            update_post_meta( $post_id, '_wpcf_belongs_impresa_id', $_POST['selectcompany'] );
        }
    }
}

- Change 12345 to match the ID of the View
- Change 67890, 98765 to be a comma-separated list of CRED form IDs (your edit and add CRED forms)
- Place the View of posts inside the options of the generic select field:

[cred_generic_field field='your-field-slug' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="your-parent-post-view-slug"] ]
}
[/cred_generic_field]
This support ticket is created 6 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 31 replies, has 2 voices.

Last updated by Silvia 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#721001

Added the "output=raw" to the code. Since the edit link on the page hidden link has disappeared, so I cannot see if its working or not. So I tried with the page "Aggiungi Opportunità Imprese" which is the page with the cred that adds a new job opportunity. Still not working. And I don't quite understand what you mean when you say "please move the View outside of the generic field so you can observe the results generated by the View".

If you could, I would be grateful if you tried to add or edit an opportunity on the front-end and then check on the back end if the association has been done correctly. If you could log in and take a look yourself It would be great because it seems ok on my end, but still... it's not working, and I REALLY need this to work. I am so grateful you're being so helpful and I am sorry to bother you so much but this is very very important for me.

#721002

I think I figured out what you asked: here it is

{"value":"178","label":"SATA SERVIZIO ASSISTENZA TECNICA ARTIGIANI"} ,{"value":"140","label":"CEDIT - CENTRO DIFFUSIONE IMPRENDITORIALE DELLA TOSCANA"} ,{"value":"141","label":"I BORGHI SRL"} ,{"value":"142","label":"IANNELLI SRL"} ,{"value":"143","label":"NESTI SAS DI LETIZIA E SABRINA NESTI & C."} ,{"value":"144","label":"TIPOGRAFIA LA MODERNA SRL"} ,{"value":"145","label":"DATAFOX SRL"} ,{"value":"115","label":"ROSSI & RICCERI SNC"}

#721234

I'm not sure why the edit link isn't showing up. I will take a closer look at that. For now, to test editing a post you can use the URL format hidden link
Just change the opim-243 part to use the correct post slug. This should show the form so you can run tests.

It looks like the generic field generated by the view you told me to create is not storing any value in the wpcf_belongs_impresa_id.
That's correct, we have not added this part yet. You must add a cred_save_data hook to accomplish this, but let's not get ahead of ourselves. We need to determine why the Edit Link stopped appearing. I will take a look and get back to you shortly.

#721357
Screen Shot 2018-04-23 at 5.19.34 PM.png

It looks like the CRED form was changed to edit "Articoli" instead of the Opportunita Imprese CPT. This is why the "Modifica" links were not appearing. I changed that and now the links are back. So please check it out and let me know if you're ready to move to the next step, which is saving the parent post reference when the CRED form is submitted.

#721359

Ok, now the edit link is back. Thanks.
I am ready to get to the next step. One question: are these steps valid for both the edit and the add child posts?

#721463

These steps apply to both adding and editing child posts, and there is one extra step in the Edit Post CRED form that selects the appropriate parent post when the page loads. I can share that later. Here is the code that will save the parent post connection:

add_action('cred_save_data', 'cred_belongs_impresa_action',10,2);
function cred_belongs_impresa_action($post_id, $form_data)
{
    $forms = array( 196, 9999 );
    // if a specific form
    if (in_array($form_data['id'], $forms))
    {
        if (isset($_POST['selectcompany']))
        {
            update_post_meta( $post_id, '_wpcf_belongs_impresa_id', $_POST['selectcompany'] );
        }
    }
}

Try this out and let me know if the parent impresa post is saved correctly.

#721478
IMAGE1.jpg

As you can see it doesn't seem to work. I added it as you wrote it. Do I have to change anything?

#721507

It just worked for me on this post:
hidden link

I can now see the parent here:
hidden link

Can you tell me which post you were editing?
Which parent post did you select?
Can you provide the login credentials for your User in the private reply fields here?

#727775

Okay you were adding a child post, not editing a child post, correct? That's a different CRED form. We didn't finish the changes necessary for adding posts yet, I was testing the edit form. For the "add" child post CRED form to work, you need to modify this line of code in functions.php:

$forms = array( 196, 9999 );

You must replace 9999 with the numeric ID of the CRED form that adds child posts. Please try that and let me know if the Add post CRED form works as expected.

#728269

Chris, it worked perfectly. I then re-edited functions.php and put it back the way it was. Ready for the next step. THANK YOU!!!

#728766

I think the next step is to update the default value of the generic field in the Edit CRED form. This will select the correct parent post by default when the page loads.

"default":[[wpv-post-id id="$impresa"]],
#728863

It's working beautifully!! Thank you so much for your precious help!
So the editing of the child posts is up and running.
Now I have to duplicate the same process for the NEW child posts. How do I go about it?

#728892

There's nothing special to do on the Add post CRED form. The code in the CRED form builder can be exactly the same as the edit form except the generic field default attribute should be:

"default":[],

This means the generic field will not have anything selected when the page loads. The cred_save_data code was already modified to support both forms, so the parent Impresa post should be saved as expected when the form is submitted. I think you should run several tests now. Creating new posts and edit those posts while logged in as different Users. Verify the parent post options list is accurately filtered, and verify the parent post is saved correctly each time in wp-admin.

#728939
2018-04-24_184011.jpg

I tried adding a new child post but the company wasn't saved. Is the function you had me add to functions.php the same for both? Shouldn't I be adding a similar function for the adding process? The cred form ids are different. In fact you had me change the 9999 to the add post cred form id, in the previous post.

add_action('cred_save_data', 'cred_belongs_impresa_action',10,2);
function cred_belongs_impresa_action($post_id, $form_data)
{
    $forms = array( 196, 9999 );
    // if a specific form
    if (in_array($form_data['id'], $forms))
    {
        if (isset($_POST['selectcompany']))
        {
            update_post_meta( $post_id, '_wpcf_belongs_impresa_id', $_POST['selectcompany'] );
        }
    }
}

I changed 9999 to 230, which is the id of the edit cred form, so I thought that for the adding process, there would be a similar function with different parameters...

#729019

I guess I figured it out for myself: I changed the parameters from this :

$forms = array( 196, 9999 );

to this:

$forms = array( 196, 230);

and now it seems to be working.

Can I please leave the ticket open while I perform tests? I would like to close it when I am sure everything is working.

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