I am trying to get a custom field value (Field "bildname") as my post title, and another (Field "bildbeschreibung") as my content in my Custom Post Type. It is not really working though. First it kept outputting "Auto Draft" for my Title. SO I started researching here, then I managed to make it visible as Title in the Backend but it didn't show up in the search when using the code from here. It seems it didn't really overwrite the Database entry.
So now I've been using this code suggested in another Forum:
//SAVE CUSTOM FIELD "TITLE" AS TITLE
add_action('save_post', 'change_title');
function change_title($post_id) {
$post_title = get_post_meta($post_id,'bildname',true);
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = $post_title;
remove_action('save_post', 'change_title');
wp_update_post( $my_post );
add_action('save_post', 'change_title');
}
// SAVE CUSTOM FIELD INPUT CONTENT AS POST CONTENT
add_action('save_post', 'change_content');
function change_content($post_id) {
$post_content = get_post_meta($post_id,'bildbeschreibung',true);
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_content'] = $post_content;
remove_action('save_post', 'change_content');
wp_update_post( $my_post );
add_action('save_post', 'change_content');
}
That seemed to be working fine, until I realized it would now also change my PAGE's titles. From the title I set there in the original title field to nothing. Because I don't have a custom field "title" for pages.
Now I need help to modify the functions I found to something, that will only affect my Custom Post Type "albertis-kunstwerke".
I found this code, that is supposed to figure out if I am on a page, or post or whatever.
function get_current_post_type() {
global $post, $typenow, $current_screen;
//we have a post so we can just get the post type from that
if ( $post && $post->post_type )
return $post->post_type;
//check the global $typenow - set in admin.php
elseif( $typenow )
return $typenow;
//check the global $current_screen object - set in sceen.php
elseif( $current_screen && $current_screen->post_type )
return $current_screen->post_type;
//lastly check the post_type querystring
elseif( isset( $_REQUEST['post_type'] ) )
return sanitize_key( $_REQUEST['post_type'] );
//we do not know the post type!
return null;
}
It's from http://themergency.com/wordpress-tip-get-post-type-in-admin/ but I don't know how to combine it with the code above so the title changes will only happen for my CPT "albertis-kunstwerke"
So, again. What I want to achieve is to make my Custom Field "bildname" (that I made with Advanced Custom Fields Plugin) my post-title. But this should only happen for Custom Post Type "albertis-kunstwerke". I need to be able to disable it for pages, since I don't have a custom field "bildname" there.
And in case any of you wonder - I can not use the regular title field, because it has been specifically requested to use all Custom Fields.