0

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.

0

The solution you are working on will always represent multiple database inserts as the post will be saved and then the the title re-saved in a second database write. That is the nature of the save_post hook, since it runs after the primary post save.

Additionally, the code you found is wildly overcomplicated.

Interestingly, the OP in the forum you linked to was on the right track in the opening post and was then misdirected, in my opinion, to a less optimal solution, which unfortunately you've followed.

The hook you should be using is wp_insert_post_data. That hook allows you to alter the post data before the initial save, which saves you a database write. The performance difference isn't going to be that much in most cases but little bits adds up and why do it second rate when first rate isn't that hard?

function meta_value_title_wpse_126764($data){
  global $_POST;
  if ('your-cpt' = get_post_type() && !empty($_POST['meta'])) {
    foreach ($_POST['meta'] as $v) {
      if ('meta_title' == $v['key']) {
        $data['post_title'] = wp_kses( $v['value'] ); // The custom content
      }
    }
  }
//   var_dump($_POST,$data); die; // debugging
  return $data; 
}
add_action('wp_insert_post_data','meta_value_title_wpse_126764',1);

This is nearly the same answer as here and has the same caveats:

  1. I can't remember at which step WordPress applies its own filters, so I applied wp_kses
  2. Item #1 may be overkill
  3. Item #1 may not be the particular validation function you want
  4. If there are multiple custom meta fields with the same key-- article_content-- the last one, and only the last one, will be used.
  • First of all, thank you for the answer. Second: I guess I don't quite understand which values I need to change to my own. 'your-cpt' is my custom post type, but where do I need to put my Custom-Field-Title? – deadfishli Dec 17 '13 at 16:02
  • In the example, I saved the alternate title as meta_title using the default custom fields meta box in the backend. You can save under whatever key you want using the default box or a meta box of your own creation. – s_ha_dum Dec 17 '13 at 17:21
1

So with s_ha_dum's Link I found some other code, that worked to both post new posts and edit old ones with the Titles saved the way I wanted. I combined that with the if-conditional in s_ha_dum's code and now it doesn't delete my page titles anymore.

So SO FAR it seems to work. I'll keep testing and hope it will work with everything else I need to do.

This is the code I am working with right now, as suggested here. If anyone has any improvements I'd be glad to hear =)

//Save ACF field as post_content for back-end
add_action('save_post', 'change_title_albertis');

function change_title_albertis($post_id) {
    global $_POST;
    if('albertis-kunstwerke'== get_post_type())
    {
        $post_custom_title = get_post_meta($post_id,'bildname',true);
        $my_post = array();
                $my_post['ID'] = $post_id;
                $my_post['post_title'] = $post_custom_title;
remove_action('save_post', 'change_title_albertis');
                    wp_update_post( $my_post );
add_action('save_post', 'change_title_albertis');
    }
   }

//Save ACF field as post_content for front-end
add_action('acf/save_post', 'change_title_frontend_albertis');

function change_title_frontend_albertis($post_id) {
    global $_POST;
    if('albertis-kunstwerke'== get_post_type())
    {
        $post_custom_title = get_post_meta($post_id,'bildname',true);
        $my_post = array();
                $my_post['ID'] = $post_id;
                $my_post['post_title'] = $post_custom_title;
remove_action('acf/save_post', 'change_title_frontend_albertis');
                    wp_update_post( $my_post );
add_action('acf/save_post', 'change_title_frontend_albertis');
    } 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.