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?
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.
Thank you Christian.
This solution works well.
Two questions
First, to do this for another column, would I need to another
such as:
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.
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.
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;
}
}
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.
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.
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.
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.
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.