WordPress.org

Ready to get started?Download WordPress

Forums

Method to update $post_title and $post_name for custom post (8 posts)

  1. pszeinert
    Member
    Posted 4 years ago #

    I am creating a custom post type to idiot-proof a portion of the admin panel. I have unhooked all default entry boxes (title, excerpt, content, etc.) and have replaced them with custom fields. Using the values of some of the custom fields, I want to dynamically create/update the $post_title and $post_name (the content is hard-coded and static as a placeholder because it is irrelevant for the specific task).

    I have been able to change the values using both wp_update_post and wp_insert_post, but because they run the save_post action it creates and infinite loop and adds a few hundred extra posts every time I publish or update.

    Currently, I am happy with the code before //Create post object

    function save_title($post_ID) {
    
    	$this_post = $post_ID;
    
    	global $post;
    
    	$custom = get_post_custom($this_post);
    	$event_team = $custom["event_team"][0];
    	$event_opponent = $custom["event_opponent"][0];
    	$event_date = $custom["event_date"][0];
    
    	$my_post_title = ($event_team ." vs. " .$event_opponent ." - " .$event_date);
    	$my_post_name = ($event_team ."-vs-" .$event_opponent ."-" .$event_date);
    
    	// Create post object
    	$custom_title = array();
    	$custom_title['ID'] = $this_post;
    	$custom_title['post_title'] = $my_post_title;
    	$custom_title['post_content'] = 'This is a custom post of the sport_event type. If you can see this text, something is wrong';
    	$custom_title['post_name'] = $my_post_slug;
    
    	// Insert the post into the database
    	wp_insert_post( $custom_title );
    }

    What method can I use to pass my variables ($my_post_title and $my_post_name) to the database ($post_title and $post_name) without creating an infinite loop?

  2. pszeinert
    Member
    Posted 4 years ago #

    Quick follow-up:

    I have added the function to the save_post action, the publish_post action and a few others. The answer may reside in where I try to add the database update in addition to how I update (i.e. wp_insert_post).

  3. kangaechigai
    Member
    Posted 3 years ago #

    Perhaps you've already solved this by now, but in case anyone else has a similar problem, I'll mention how I got past it in my case. Instead of attaching to any actions, I used the title_save_pre and name_save_pre filter hooks. Something like this might work in place of the code above:

    add_filter('title_save_pre', 'save_title');
    function save_title($title_to_ignore) {
    	global $post;
    	$custom = get_post_custom($post->ID);
    	$event_team = $custom["event_team"][0];
    	$event_opponent = $custom["event_opponent"][0];
    	$event_date = $custom["event_date"][0];
    	$my_post_title = $event_team . " vs. " . $event_opponent . " - " . $event_date;
    	return $my_post_title;
    }
    
    add_filter('name_save_pre', 'save_name');
    function save_name($name_to_ignore) {
    	global $post;
    	$custom = get_post_custom($post->ID);
    	$event_team = $custom["event_team"][0];
    	$event_opponent = $custom["event_opponent"][0];
    	$event_date = $custom["event_date"][0];
    	$my_post_name = $event_team . "-vs-" . $event_opponent . "-" . $event_date;
    	return $my_post_name;
    }
  4. McSalty
    Member
    Posted 3 years ago #

    kangaechigai, your solution works... except it only works when the post is being updated. (I.E. It doesn't work when I first click "publish" for a new post. I have to click "publish", then click "update" for it to work).

    I think the problem is get_post_custom() is polling the database, but this data hasn't been saved yet since 'name_save_pre' fires BEFORE the data is stored in mySQL.

    I'm far from an expert here, but I think it's possible we can access all of this data from within the $_POST array... I'm going to hack around with it, and post here if I figure anything out.

  5. McSalty
    Member
    Posted 3 years ago #

    Ah, that was fast. Here's the code I'm using. It's working perfectly

    add_filter('name_save_pre', 'save_name');
    function save_name($my_post_name) {
            if ($_POST['post_type'] == 'parties') :
              $party_date = $_POST['date'];
              $party_name = $_POST['party_name'];
              $my_post_name = $party_name . "-" . $party_date;
            endif;
            return $my_post_name;
    }

    I check to see if the post type is 'parties', and if so I change the URL based on two custom fields called 'date' and 'party_name'.

    Again, I just hacked this together, so someone let me know if there are any potential problems with the way I'm achieving this.

  6. nelsonkoehn
    Member
    Posted 3 years ago #

    Wanting to do the same thing and followed your lead. Thought it was working until it changed my 'Publish' button to a 'Submit for Review' button even for the administrator. Also I cannot move my custom post type to the trash, it will not delete. Commented out my add_filter line and now buttons and trash work right but no title.
    This is the code I am using.

    add_filter('title_save_pre', 'my_event_save_title');
    
    function my_event_save_title($title_to_ignore) {
        if ($_POST['post_type'] == 'my_event') :
              $date = date('m-d-y',strtotime($_POST['_date']));
              $name = $_POST['_title'];
              $my_post_title = $name;
            endif;
            return $my_post_title;
    }

    Have you noticed this or do you have any ideas

  7. McSalty
    Member
    Posted 3 years ago #

    What exactly is "$_POST['_title']"? Is "_title" a custom field you're filling in? If so, why not just use wordpress's title instead of the custom field?

    Try commenting out your function and replacing it with "print_r($_POST);" or "var_dump($_POST);". When you click "publish" in wordpress, it will take you to a page where it write out the entire contents of the $_POST array, so you can see what values you have available to work with.

    Lastly, try changing "$title_to_ignore" to "$my_post_title"? I haven't tested it, but it looks like in your example if post type is NOT my_event, you're attempting to return a blank string. Maybe this would prevent wordpress from allowing you to save at all?

  8. nelsonkoehn
    Member
    Posted 3 years ago #

    $_POST['_title'] and $_POST['_event'] are custom fields from a custom post type of events. A lot of my events have the same title so did not want this in the title line but a combination of event title and event date. (My main title box is hidden). I want it this way so in the list view of my custom posts I have the combination title/date in the title column so that the link works to the edit post and also has Edit|Quick Edit|Trash|View available.
    I did have a typo in my code above so that it didn't make a lot of sense
    $my_post_title = $name;
    should be changed to
    $my_post_title = $name . "-" . $date;
    It appears as if changing $title_to_ignore to $my_post_title has fixed the issue of submit vs publish. This also will return the original string other than on my custom post type, right?

Topic Closed

This topic has been closed to new replies.

About this Topic