CMB2 / CMB2-Post-Search-field

Custom field for CMB2 which adds a post-search dialog for searching/attaching other post IDs
67 stars 27 forks source link

Feature request - Show titles instead of just ID in field #10

Open cggit opened 9 years ago

cggit commented 9 years ago

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.

monecchi commented 8 years ago

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.

tw2113 commented 8 years ago

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.

monecchi commented 8 years ago

Actually what I mean about returning a title along with the ID is:

but

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.

jtsternberg commented 8 years ago

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.

monecchi commented 8 years ago

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

vaclavgreif commented 7 years ago

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

anmamtl commented 7 years ago

+1

bake commented 6 years ago

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 );
cupcakedream commented 6 years ago

+1

jtsternberg commented 6 years ago

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.

davelavoie commented 4 years ago

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

pgroot91 commented 4 years ago

@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 commented 4 years ago

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