CMB2-Post-Search-field icon indicating copy to clipboard operation
CMB2-Post-Search-field copied to clipboard

Feature request - Show titles instead of just ID in field

Open cggit opened this issue 9 years ago • 13 comments

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.

cggit avatar Aug 23 '15 01:08 cggit

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.

monecchi avatar May 26 '16 11:05 monecchi

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.

  1. Titles can get long and unwieldy, combine that with a spot where you can store multiple values, and it risks getting more confusing.
  2. 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.

tw2113 avatar May 26 '16 15:05 tw2113

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.

monecchi avatar Jun 01 '16 05:06 monecchi

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 avatar Jun 01 '16 06:06 jtsternberg

@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.

monecchi avatar Jun 01 '16 06:06 monecchi

+1 for this, I'm using the plugins a lot and displaying post ID is not user friendly

vaclavgreif avatar Sep 01 '17 13:09 vaclavgreif

+1

anmamtl avatar Sep 28 '17 05:09 anmamtl

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 );

bake avatar Nov 12 '17 11:11 bake

+1

cupcakedream avatar Jul 17 '18 15:07 cupcakedream

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.

jtsternberg avatar Jul 17 '18 16:07 jtsternberg

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 avatar Jan 20 '20 15:01 davelavoie

@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.

pgroot91 avatar Jan 20 '20 17:01 pgroot91

@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.

davelavoie avatar Jan 20 '20 17:01 davelavoie