CMB2-Post-Search-field
CMB2-Post-Search-field copied to clipboard
Feature request - Show titles instead of just ID in field
Would be helpful to get a post title in the field instead of id for content editors.
I think the field should still return the id so maybe using JS you could replace the id with a title and little x to delete it. Similar to how tags work in WordPress and Stackoverflow.
I like the idea of displaying a title. Perhaps, after the ID has been attached to the field, the plugin could return a simple label right bellow the search input field with the post title such as <label>The Attached Post Title</label>
.
It makes no sense at all for a user to only see the ID, nobody knows by memory each post ID, so displaying a title should save you a click on the search button just to recall the name of the attached post.
I am not the final authority on the plugin nor do I know why the decision was made this way, but I have to believe it was done this way for a couple reasons.
- Titles can get long and unwieldy, combine that with a spot where you can store multiple values, and it risks getting more confusing.
- Easier to do the querying with the post ID. I realize you're not talking about replacing the ID completely. Except in certain types of imports when migrating content, the ID is a lot more likely to remain consistent than the title, which may be tweaked for various reasons. Once that's changed querying by title saved in meta gets broken.
More than willing to be wrong, or have ideas proposed that perhaps makes the best of both worlds. I just wanted to bring up my thoughts on why it is what it is now.
Actually what I mean about returning a title along with the ID is:
- Do not mess with the existing ID functionality, it is indeed the best way to keep things running consistent in the long run.
but
- Get the title from the attached post ID and display it right bellow the search input field. It not only would add support for a "human" readable identifier, as it would enhance the user experience.
For longer titles, a simple WP function could cut that off, enough to make the post visually recognizable.
e.g:
<?php echo wp_trim_words( get_the_title(), 120, '...' ); ?>
The plugin is great as is, and perhaps the suggested functionality is not that relevant for the "wp admin" level peeps, for most site owners who lack wp authoring skills it'd be a considerable nice enhancement.
It'd be fairly trivial to add, and is in place in several of the other field-types which do something similar. Just need to get the time.
@jtsternberg that would be great. I've tried to bring that functionality myself, but as I'm not a developer, I'm afraid I lack the skills to make it right.
+1 for this, I'm using the plugins a lot and displaying post ID is not user friendly
+1
I usually just add a list of the posts after the input. This obviously isn't updated live.
add_action( 'cmb2_render_post_search_text', function( $field ) {
$args = [
'post_type' => $field->args['post_type'],
'post_count' => -1,
'orderby' => 'post__in',
'post__in' => explode( ',', $field->escaped_value ),
];
$posts = get_posts( $args );
foreach ( $posts as $post ) {
printf( '%d: <a href="%s">%s</a><br>', $post->ID, get_edit_post_link( $post->ID ), $post->post_title );
}
}, 20 );
+1
I'd recommend switching to the CMB2 Attached Posts Field, as it has the same search feature, but also other UI enhancements like drag & drop and filtering.
Hi everyone,
I've forked the repo and added support for displaying the Title instead of the ID along with a few other improvements. If you want to use it simply go to https://github.com/EP4/CMB2-Post-Search-field
@davelavoie can you update the read-me with the changes in your fork? For people it is easier to distinguish all those forks from one and another.
@davelavoie can you update the read-me with the changes in your fork? For people it is easier to distinguish all those forks from one and another.
Sure, I've just updated it.