-
Notifications
You must be signed in to change notification settings - Fork 27
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
Comments
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. |
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. |
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. |
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? |
I hacked up a 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 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
I haven't tested it with @jtsternberg Is this something you're interested in? If so, I'll take any feedback up front and send a pull request. |
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. |
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.
The text was updated successfully, but these errors were encountered: