Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Set post title to taxonomies WordPress

  • SOLVED

This was originally a StackExchange question that I need the answer to asap.

I have a custom post type called inventory that uses four taxonomies to create a sudo-title tag on the front end. I need a ready to go function that will rename the post title from "auto draft" to the 3 taxonomies on post publish. If in the rare instance the post title is still "auto draft" and the post is published it should be renamed when the post is manually updated.

@baininternet got the code started but there is an error in his code.

The 3 taxonomies that will be used, in order are: year, make, model.
So for example the title would be set to 2010 Jeep Wrangler

Thanks all!


[[LINK href="http://wordpress.stackexchange.com/questions/15157/how-to-make-custom-post-type-feed-title-taxonomies"]][[/LINK]]

Answers (3)

2011-04-21

Lew Ayotte answers:

Try this for the first part...

function custom_update_post_title( $post ) {

if ( 'inventory' != $post->type )
return false;

$years = wp_get_object_terms( $post->ID, 'year' );
if ( !empty( $years ) ) {
$year = $years[0]->name;
} else {
return false;
}

$makes = wp_get_object_terms( $post->ID, 'make' );
if ( !empty( $makes ) ) {
$make= $makes[0]->name;
} else {
return false;
}

$models = wp_get_object_terms( $post->ID, 'model' );
if ( !empty( $models ) ) {
$model= $models[0]->name;
} else {
return false;
}

$title = $year . ' ' . $make . ' ' . $model;

$postarr = array(
'post_title' => $title,
'ID' => $post->ID
);

wp_update_post( $postarr );

}
add_action( 'edit_form_advanced', 'custom_update_post_title', 20 );
add_action( 'edit_page_form', 'custom_update_post_title', 20 );
add_action( 'save_post', 'custom_update_post_title', 20 );
add_action( 'new_to_publish', 'custom_update_post_title', 20 );
add_action( 'draft_to_publish', 'custom_update_post_title', 20 );
add_action( 'pending_to_publish', 'custom_update_post_title', 20 );
add_action( 'future_to_publish', 'custom_update_post_title', 20 );


I haven't tested it, but it should work.

and for your 'auto draft' title (if a mistake happens) try this...

function auto_draft_check( $title, $id ) {
$post = &get_post( $id );

if ( 'inventory' != $post->type )
return $title;

if ( 'auto draft' == $title )


$years = wp_get_object_terms( $post->ID, 'year' );
if ( !empty( $years ) ) {
$year = $years[0]->name;
} else {
return $title;
}

$makes = wp_get_object_terms( $post->ID, 'make' );
if ( !empty( $makes ) ) {
$make= $makes[0]->name;
} else {
return $title;
}

$models = wp_get_object_terms( $post->ID, 'model' );
if ( !empty( $models ) ) {
$model= $models[0]->name;
} else {
return $title;
}

$title = $year . ' ' . $make . ' ' . $model;

}

return $title;

}
add_filter( 'the_title', 'auto_draft_check', 10, 2 );


Lew Ayotte comments:

If you want to contact me on AIM or GCHAT as lewayotte, I can probably help you better


Lew Ayotte comments:

I had a couple typo's in there, updated the answer


Matt Taylor comments:

The code isn't working for me. What do you recommend?


Matt Taylor comments:

note: Lew fixed this code directly

2011-04-21

Sébastien | French WordpressDesigner answers:

have you a link to StackExchange ?

2011-04-21

Maor Barazany answers:

I'm not sure I understood exactly what needed.
For each post you have 3 taxonomies , that each of them will have one term.
The three of the erms should appear in the front end as the title?

Should it be used as the post's title in the backend also, or only in front end site?

If only in the front site, you can go to your theme's single.php file and replace inside the loop the call to <strong>the_title() </strong> function with something like this:

<?php
$year_tax = wp_get_post_terms($post->ID, 'year');
$make_tax = wp_get_post_terms($post->ID, 'make');
$model_tax = wp_get_post_terms($post->ID, 'model');

$year = $year_tax[0]->name;
$make = $make_tax[0]->name;
$model = $model_tax[0]->name;

$new_title = $year . ' ' . $make . ' ' . $model;

echo $new_title
?>


Matt Taylor comments:

The question was for changing the title in the database to the taxonomies.