post_title and post_name from custom fields and taxonomies
-
Sorry if this is too long, I just want to be as clear as possible
I am creating a theme for my blog with some custom post types and custom meta boxes and taxonomies, all of them created in functions.php. I’d like to optimize the input of data, as some other people will be posting to the blog, so we all use the same formats
So, for example, one of the custom post types is “concerts”, where we talk about concerts we’ve been to. When people visit one of these concert reviews at our site, we would like them to see the name of the band, which is clickable and links to that band’s archive, the name of the venue and the date of the concert. All of them in different lines and styles, hence the custom meta box with fields for “venue name” and “date” and the custom taxonomy for “band”.
Typically, when posting, we would have to write down all these fields, plus the title box, where we would write those three items again. Essentially, we don’t want the title at all, we would just like to have both the post title and the post name generated from the custom fields and taxonomies
So, I disabled “title” in supports when creating the custom post type, and used this piece of code I found to achieve our goal. It is working for the post title, which I can see on top of my browser, but I keep getting a 404 error when I write https://www.mysite.com/band-venue-date
function custom_post_type_title ( $post_id ) { global $wpdb; if ( get_post_type( $post_id ) == 'concerts' ) { $terms = wp_get_object_terms($post_id, 'band'); $band= ' '.$terms[0]->name; $venue= ' - '.get_post_meta($post_id, 'venue', true); $date= ', '.get_post_meta($post_id, 'date', true); $title = $band.$venue.$date; $where = array( 'ID' => $post_id ); $wpdb->update( $wpdb->posts, array( 'post_title' => $title,'post_name' => $title ), $where ); } } add_action( 'save_post', 'custom_post_type_title' );
It must be something with permalinks, but I don’t know what. I checked phpmyadmin and in the database the post status is published
Well, it is a bit long, but I needed to clearly explain my problem, because all I get about post name and permalinks when I google is about the settings->permalinks menu
Thanks in advance
[ Please do not bump, that’s not permitted here. ]
- The topic ‘post_title and post_name from custom fields and taxonomies’ is closed to new replies.