Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HELP: Need guidance on how to return slugs instead of Post IDs. #14

Open
hallcp2 opened this issue Oct 1, 2015 · 6 comments
Open

HELP: Need guidance on how to return slugs instead of Post IDs. #14

hallcp2 opened this issue Oct 1, 2015 · 6 comments

Comments

@hallcp2
Copy link

hallcp2 commented Oct 1, 2015

It would be nice to return slugs instead of ID numbers. Can someone give me a hint as to where to start on making this change? I've been staring at the code for days and it's just over my head. I know Javascript and PHP but I am not a guru on the WP API or the CMB2 API.

Thanks.

@billerickson
Copy link

What's your use case? Why do you need a slug, to provide a URL? Use get_permalink( $post_id ) to get the full URL to the post.

Slugs can change change over time but post IDs won't.

@hallcp2
Copy link
Author

hallcp2 commented Oct 1, 2015

Well, permalinks would do too. We are using the Ramp plugin to move this content to another blog so the post IDs will need to change. And like the previous issue someone put in, the users don't think of posts in terms of 4-digit numbers, so the permalink would be more meaningful for them. But our immediate problem is we need to either modify your plugin, or Ramp, to have things work when they're moved over to the production site.

Thanks so much for the feedback!

Charles.

@billerickson
Copy link

What I would do in that situation is hook into save_post and add another meta field that stores either the page title ( get_the_title( $post_id ) ) or page path ( get_page_uri( $post_id ) ).

Then, when the content is migrated over, go through all posts that have that meta key and use either get_page_by_title( $title) or get_page_by_path( $path ) to get the post ID for that install and update the relevant meta field.

Or you could simply modify the field to save the title/path, but that would be less reliable in production since users could change titles/paths.

@hallcp2
Copy link
Author

hallcp2 commented Oct 2, 2015

There's a real usability benefit to having something more memorable than a Post ID in this field. I am able to convert the IDs to Permalinks using the sanitize hook. But I don't know how to increase the size of the text box so it can show such long strings. I tried changing the field type to textarea, but there must be more to it (or I don't know what I'm doing!!) since although things change, the visible portion remains restricted to one line as before. Can you give me any pointers on increasing that area?
And thanks for the post_save hook suggestion. That idea is very interesting.

@willthemoor
Copy link

I hacked up a <airquote>fix</airquote> for this involving a second label for the input with get_post_title() shoved in it. Then, hiding the input entirely and making the label look like an input.

I'll push it in case anyone wants to pilfer from it but thought I should ask first if there's a 'correct' way to attach a secondary label to $field_type->input? I have no idea! Feels a little dirty but I've not done this before. It basically starts off like this:

    function cmb2_post_search_render_field( $field, $escaped_value, $object_id, $object_type, $field_type ) {
        if ( $field->args('use_post_title') ) { // use_post_title is a true/false boolean
            $title =  !empty($field->escaped_value) ?  get_the_title($field->escaped_value) : "";
            echo "<label class='attached-post-title' for='" . $field->args('id') . "'>" . $title . "</label>";
        }
        echo $field_type->input( array //.... existing code...

The rest basically breaks down like this (if use_post_title=>true):

  • Added JS to get the label to update when a post is selected in the modal.
  • Added CSS to make the label look like an input (taking up the same space). Bonus: since it's not actually an input, the label is happy to grow vertically with absurdly long titles.
  • Added CSS to hide the ID text input. Seems like it might be cleaner to set the input to type=hidden but I'm not sure.
  • Added a little more JS to open the modal when the label is clicked instead of the search button only.

I haven't tested it with select_behavior=>"add" yet. replace only at this point.

@jtsternberg Is this something you're interested in? If so, I'll take any feedback up front and send a pull request.

@willthemoor
Copy link

Added CSS to make the label look like an input (taking up the same space). Bonus: since it's not actually an input, the label is happy to grow vertically with absurdly long titles.

Came across a problem with this method today: there's no way to empty the field (remove a post association) since it's not actually a form field but a label masquerading as one.

Not sure if I'm going to add a clear button or try to add a null option to the pop up but in case anyone actually tries going down the path outlined above... fyi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants