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

Sure, I will mark this ticket as pending an update from you. No need to reply right now. The ticket will remain open for 30 days.

#784605

Chris, I haven't had time to test the solution thoroughly because I needed to have a meeting with the client in order to discuss the matter with them, but they kept postponing it. From what I could actually test, the solution you provided works, so I will close this ticket, knowing that I can always open another one if something comes up. Thank you for your patience and your precious precious help.

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