Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to optimize the code of my site . I wanted to know if it was possible to give automatically a title to a new post without having to do it manually. I have a website of video games. Before posting a review, I first need to create the plug of this game with all the information (publisher, developer, pc configuration etc.).. So I have to put twice the same title. I would only select the plug of the game without having to type the title of the game

To get the title, I am able to retrieve the ID of the plug. Do you think I can write a function?

Thank You

Code example for get the title of a game :

<?php foreach(get_field('fiche') as $post_object): ?>
<? echo get_the_title($post_object->ID); ?><br />
<? echo 'Genre' .get_field('genre', $post_object->ID); ?>
<? endforeach; ?>
share|improve this question
1  
Yes, Shingo, it's possible to write a function. But first, please give more details in your Question (edit). 1) What relationship field? 2) You have to show the code you are working with. 3) What did you find while searching this site? Tip: +save_post +"custom field". –  brasofilo Jan 13 '13 at 13:10
    
is plug===slug? –  Mark Kaplun Jan 13 '13 at 13:19
    
Thank you. So no, "plug" is a post from a custom post type. Here's a plug of a game on my site: cobra-system.com/fiche/dead-rising-2-off-the-record It is from there that all the information I get a game for other articles (review, news, etc..) Custom relationship field is a field created with the plugin Advanced Custom Field (ACF). It allows you to select other related post. The relationship stoke the custom field id in an array. For the record this is an array with a single ID.For the moment no, I have not written anything like code because I have no idea how to do it. –  Shingo Jan 13 '13 at 13:51

1 Answer 1

up vote 0 down vote accepted

If you can grab your data early enough you can do this:

function alter_title($t) {
  return 'altered-title';
}
add_filter('pre_post_title','alter_title');
add_filter('pre_post_name','alter_title');

That will change the title and the slug before the post is saved. Of course, you need to work out some logic for that function. As is, it changes all post/page/CPT names and slugs on both save and edit. Probably not what you want... :)

That should work. Your game data should be in $_POST. If for some reason it doesn't you can run a function on save_post but you will need to push another write to the database.

There are a lot of hooks for post insert/edits so you may even find something more appropriate by browsing the source file.

share|improve this answer
    
I managed to adapt the code that works very well. For cons, I have to update the article to the title appears. This is normal because the relationship field has not saved the id of the game in the first time. I do not know if we can work around the problem but I'm glad it can operate –  Shingo Jan 13 '13 at 17:24
    
If you want it to update dynamically you will need to do it with Javascript, but don't remove the PHP. Javascript is for convenience only. It can be disabled or altered by the end user, or just broken if a plugin or mistake causes a fatal error. –  s_ha_dum Jan 13 '13 at 17:31
    
If you give a small example in javascript, I'm interested because I'm not at all familiar with this language. I just PHP and C # ... –  Shingo Jan 13 '13 at 18:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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