Skip Navigation

[Resolved] Configuring Post Fields display in WordPress Admin to show Display Text

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

Problem: I would like to show custom field text labels in wp-admin post lists, but I see both the text label and the option value.

Solution: We have identified the solution for this specific example and will include it in an upcoming release. However, it should be noted that types_render_field is not intended for use in the back-end of the site and may have unexpected results elsewhere.

This support ticket is created 6 years, 9 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
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 9 replies, has 2 voices.

Last updated by georgeF-2 6 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#621736
wfaco_types_03.JPG
wfaco_types_02.JPG
wfaco_types_01.JPG

Tell us what you are trying to do?

I have a custom Post Type called Residences. I'd like to display a few Post Fields in the WordPress Admin to help with filtering. It works fine, but a few of the Select fields have Display Text that I would prefer to display, such as Community (Easier to end user to see "Springfield, Downtown, Suburb X" instead of "1, 2, 3"

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#621855

You can filter the column values with some custom code. Add this code to your child theme's functions.php file:

add_action( 'manage_residence_posts_custom_column', 'manage_residence_posts_column_func', 10, 2 );

function manage_residence_posts_column_func( $column, $post_id ) {

    require_once WPCF_EMBEDDED_ABSPATH . '/frontend.php';

    switch( $column ) {

        case 'wpcf-community' :
          echo types_render_field("community", array('id' => $post_id));
        break;

        default :
        break;
    }
}

If your custom post type "Residences" slug is not "residence", you must replace all 3 instances of the word "residence" with the correct slug.

#625291
wpcf.JPG

Thank you Christian.

This solution works well.

Two questions

First, to do this for another column, would I need to another

switch($column) {...

such as:

 } switch($column2){ ...

or would I rather add a new case to the existing switch routine, say for my wpcf-school-district field?

Second question, I believe the echo line controls the output, and displays the Community Display Text followed by the post ID. Is that second part necessary?

 , array('id' => $post_id)

If so, to add in a space between the two values, would I need to insert another comma and value, or something else? Right now the output places both values adjacent without a space. It's kind of nice to have the ID there, although I'd like to get a space in if I decide to keep it there long term. Attached a visual of the output in my Admin area for Residences to the post.

#625391

or would I rather add a new case to the existing switch routine, say for my wpcf-school-district field?
Correct, add a new case to the existing switch.

Second question, I believe the echo line controls the output, and displays the Community Display Text followed by the post ID. Is that second part necessary?
What you're seeing on the front-end is the custom field text, followed by the custom field value. Look in your first screenshot above to see what I'm talking about - 1 is not the post ID, it's the value of the Bairsford option in your custom field.

The second part is required, and it's not there to display the post ID or field value. It's there to provide options into the types_render_field function. Without it, the types_render_field function doesn't know which post contains the field you want to render, or what options you want to provide that function. Check the documentation for this function and field here:
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://toolset.com/documentation/customizing-sites-using-php/functions/#select

If you're seeing the selected option text and value instead of just text, this could indicate a problem. Please copy + paste the entire function here for me to review.

#625416

Hi Christian,

Thank you - you are correct there, I was using the term PostID improperly. I did mean to say something closer to "value for the corresponding community option selected" that we were swapping the Display Text out for. And that sentence isn't much better now that I re-read it, but I do understand what you are saying.

We're seeing both ... the Display Text and then the Value of the selected option.

Here's what we have in the functions.php file

<?php

// Defines
define( 'FL_CHILD_THEME_DIR', get_stylesheet_directory() );
define( 'FL_CHILD_THEME_URL', get_stylesheet_directory_uri() );

// Classes
require_once 'classes/class-fl-child-theme.php';

// Actions
add_action( 'fl_head', 'FLChildTheme::stylesheet' );

// Configuring Post Fields display in WordPress Admin to show Display Text
// 
add_action( 'manage_residence_posts_custom_column', 'manage_residence_posts_column_func', 10, 2 );
 
function manage_residence_posts_column_func( $column, $post_id ) {
 
    require_once WPCF_EMBEDDED_ABSPATH . '/frontend.php';
 
    switch( $column ) {
 
        case 'wpcf-community' :
          echo types_render_field("community", array('id' => $post_id));
        break;
 
        default :
        break;
    }
}


#625439

Can you temporarily disable all plugins except Types and try again? If the column still shows both text and ID, I'll need to take a closer look. I will activate private reply fields so you can share login credentials in confidence.

#625477

Okay thanks, I'm able to reproduce this issue locally if there is more than one custom field column shown in the admin screen. If I remove all custom columns, I can add the column correctly with the following custom code:

//add_filter('manage_edit-residence_columns', 'manage_residence_columns_head');
function manage_residence_columns_head($defaults) {
  $defaults['wpcf-community'] = 'Community';
  return $defaults;
}

But as soon as I turn on other custom field columns the value is displayed again along with the text. I think this could be a bug, so I'm escalating to my 2nd tier support team for further clarification. Please stand by and I will update you when I have some information to share.

#625478

Ok, sounds good. I'll hang tight on any changes to this particular area (like adding in school district) while the field value thing is researched further.

We're moving forward with some development in other areas on the site, nothing that I'd expect would affect your work.

#628048

I have an update from our developers. There is some good news and some bad news. The good news is that we have implemented a fix for this specific problem that will be included in the next stable release of Types. The bad news is that types_render_field is not intended for use in the backend of the site. Even though we fix this specific location, the same function may not work as expected in other areas of the backend. Be cautious about implementing this type of custom code in wp-admin using types_render_field.

#628322

Thank you Christian. That is mostly good news. It's a very useful field for the admin columns, and I'll make sure that I don't make too many plans to use it elsewhere in the admin zone.